0

As in title - I'd like to set a cookie when any product is added to the cart and remove it if cart is empty.

I found a somewhat similar answer for setting cookies on login, which is perfect, cause I'd like to do that as well, but have no clue how to set equivalent for chart items (I'm new to Magento).

version 1.9.2.4

0

You can use the event controller_front_send_response_before in the frontend area, check if there are items in cart (using the checkout/cart helper) and set or delete cookie. A possible function for that observer could be the following:

public function setCartDataCookie($observer)
{
    $cookieName = "YOUR_COOKIE_NAME";
    $cookie = Mage::getSingleton("core/cookie");
    $cookieData = $cookie->get($cookieName);

    $cart = Mage::helper('checkout/cart')->getItemsQty();

    if ($cart > 0){
        //set cookie, when items are in cart
        $cookie->set($cookieName,"YOUR DATA");
    } else {
        //cart is empty -> remove cookie
        if ($cookieData){
            $cookie->delete($cookieName);
        }
    }
}

If you need data about the cart items in the cookie you may use Mage::helper('checkout/cart')->getQuote()->getAllVisibleItems() to get that information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.