Jump to content

Edit History

zmora.vip

zmora.vip

Czy mógłbym prosić o pomoc w tworzeniu COOKIES?
 

Rozumiem, że tym sposobem odnoszę się do głównego cookie, a później mogę dopisywać mu nowe wartości.

$cookie = Context::getContext()->cookie;
$cookie->__set("var", "value 1");
$cookie->write();

Jednakże w ten sposób nadając $cookie->setExpire(time() + 1 * 10); nadaje ważność dla całego ciasteczka.

Rozumiem też, że mogę stworzyć 

$cookie = new Cookie("ciastko"); 
$cookie->setExpire(time() + 20 * 60);
$cookie->variable_name = 'hello';
$cookie->write();

I tutaj nie mam problemu w ustawieniu terminu ważności ale jak wtedy mogę się odwołać do niego by zweryfikować czy istnieje?
Jak ustawić $cookie->__isset()?

Mam na celu stworzenie ciastka i ustawić jego ważność na np. 5 minut i móc odnieść się czy on istnieje.

zmora.vip

zmora.vip

Witajcie, dłubie i nie mam już pomysłu więc proszę o pomoc.

W classie SpecificPrice.php w funkcji getSpecificPrice() zrobiłem warunek sprawdzający czy $_GET jest przekazywany w linku i jeśli tak to tworzy setcookie. Potem sprawdzany jest czy ten cookie istnieje i jeśli tak to podmienia zmienna $id_shop by wyświetlić inna cene.

 

public static function getSpecificPrice(
        $id_product,
        $id_shop,
        $id_currency,
        $id_country,
        $id_group,
        $quantity,
        $id_product_attribute = null,
        $id_customer = 0,
        $id_cart = 0,
        $real_quantity = 0
    ) {        
        $idtmp = 123; //202cb962ac59075b964b07152d234b70
        $hash = md5($idtmp); //$hash = md5('YIN:'.$id_product);
                        
        if(isset($_GET[$hash]) and !isset($_COOKIE[$hash])){
            //setcookie($hash, '1', time()+3600*24); //dzień
            setcookie($hash, '1', time()+300);
        }
        if(!isset($_COOKIE[$hash]) and !isset($_GET[$hash])) {
            //$id_product = 0;
            //$id_shop = 0;
        }
        
        if(isset($_COOKIE[$hash]) or isset($_GET[$hash])){ 
            $id_shop = 100;
        }
        
        if (!SpecificPrice::isFeatureActive()) {
            return [];
        }
        /*
         * The date is not taken into account for the cache, but this is for the better because it keeps the consistency
         * for the whole script.
         * The price must not change between the top and the bottom of the page
        */

        if (!self::couldHaveSpecificPrice($id_product)) {
            return [];
        }
        //if(isset($_COOKIE[$hash]))
            //echo "jest2";
        //else
            //echo "!NIE MA!";
            
        if (static::$psQtyDiscountOnCombination === null) {
            static::$psQtyDiscountOnCombination = Configuration::get('PS_QTY_DISCOUNT_ON_COMBINATION');
            // no need to compute the key the first time the function is called, we know the cache has not
            // been computed yet
            $key = null;
        } else {
            $key = self::computeKey(
                $id_product,
                $id_shop,
                $id_currency,
                $id_country,
                $id_group,
                $quantity,
                $id_product_attribute,
                $id_customer,
                $id_cart,
                $real_quantity
            );
        }

        if (!array_key_exists($key, self::$_specificPriceCache)) {
                        
            $query_extra = self::computeExtraConditions($id_product, $id_product_attribute, $id_customer, $id_cart);
            if ($key === null) {
                
                // compute the key after calling computeExtraConditions as it initializes some useful cache
                $key = self::computeKey(
                    $id_product,
                    $id_shop,
                    $id_currency,
                    $id_country,
                    $id_group,
                    $quantity,
                    $id_product_attribute,
                    $id_customer,
                    $id_cart,
                    $real_quantity
                );
            }
                        
            $query = '
			SELECT *, ' . SpecificPrice::_getScoreQuery($id_product, $id_shop, $id_currency, $id_country, $id_group, $id_customer) . '
				FROM `' . _DB_PREFIX_ . 'specific_price`
				WHERE
                `id_shop` ' . self::formatIntInQuery(0, $id_shop) . ' AND
                `id_currency` ' . self::formatIntInQuery(0, $id_currency) . ' AND
                `id_country` ' . self::formatIntInQuery(0, $id_country) . ' AND
                `id_group` ' . self::formatIntInQuery(0, $id_group) . ' ' . $query_extra . '
				AND IF(`from_quantity` > 1, `from_quantity`, 0) <= ';

            $query .= (static::$psQtyDiscountOnCombination || !$id_cart || !$real_quantity) ? (int) $quantity : max(1, (int) $real_quantity);
            $query .= ' ORDER BY `id_product_attribute` DESC, `id_cart` DESC, `from_quantity` DESC, `id_specific_price_rule` ASC, `score` DESC, `to` DESC, `from` DESC';
            
            self::$_specificPriceCache[$key] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query); ///// TUTAJ NIE WYKONUJE
                
        }
        
        return self::$_specificPriceCache[$key];
    }

Wszystko wyświetla dobrze ale po naciśnięciu "dodaj do koszyka" dodaje inna wartość. Gdy w kodzie ustawi na stałe $id_shop = 100; to do koszyka również poprawnie wpada wartość. Funkcja wywoływana jest wielokrotnie ale najwyraźniej w pierwszych uruchomieniach "nie potrafi" sprawdzić czy jest coś w $_get lub $_cookie, a dopiero potem jest w stanie.

Proszę o pomoc lub inna wskazówkę jak uruchomić specjalna cenę, gdy istnieje ciasteczko.

Pozdrawiam

×
×
  • Create New...