1

Here's an interesting one, I have an observer on the customer_logout event:

<customer_logout>
    <observers>
        <vendor_module_customer_logout>
            <class>module/observer</class>
            <method>updateCartCountCookie</method>
        </vendor_module_customer_logout>
    </observers>
</customer_logout>

Then in my observer I have the following:

public function updateCartCountCookie(Varien_Event_Observer $observer)
{      
    Mage::getSingleton('core/cookie')->set(
        $name       = "cart-count",
        $value      = Mage::helper('checkout/cart')->getSummaryCount(),
        $period     = null, // default 3600
        $path       = "/",
        $domain     = null,
        $secure     = null,
        $httponly   = null
    );
}

Following this process:

  • (logged out) add stuff to my cart
  • login
  • items are still in my cart
  • logout
  • items are still in my cart

Expected result: my cart should be empty, why is it still containing all my items ?

1

The Bug

Yet another Magento bug unfortunately.

The customer_logout event is triggered before the customer is really logged out:

public function logout()
{
    if ($this->isLoggedIn()) {
        Mage::dispatchEvent('customer_logout', array('customer' => $this->getCustomer()) );
        $this->_logout();
    }
    return $this;
}

Thus when I call Mage::helper('checkout/cart')->getSummaryCount(), the following code from Mage_Checkout_Model_Cart is called:

public function getSummaryQty()
{
    $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();

    //If there is no quote id in session trying to load quote
    //and get new quote id. This is done for cases when quote was created
    //not by customer (from backend for example).
    if (!$quoteId && Mage::getSingleton('customer/session')->isLoggedIn()) {
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
    }

    if ($quoteId && $this->_summaryQty === null) {
        if (Mage::getStoreConfig('checkout/cart_link/use_qty')) {
            $this->_summaryQty = $this->getItemsQty();
        } else {
            $this->_summaryQty = $this->getItemsCount();
        }
    }
    return $this->_summaryQty;
}

As the customer is still logged in and there is not $quoteId, the system gets a new quote by calling $quote = Mage::getSingleton('checkout/session')->getQuote(); which basically retrieves the logged in customer quote.

The Solution

The solution is to dispatch the customer_logout event once the customer is logged out:

public function logout()
{
    if ($this->isLoggedIn()) {
        $this->_logout();
        Mage::dispatchEvent('customer_logout', array('customer' => $this->getCustomer()) );
    }
    return $this;
}

Even better, the solution the Magento team should implement is to split the customer_logout into two different events:

public function logout()
{
    if ($this->isLoggedIn()) {
        Mage::dispatchEvent('before_customer_logout', array('customer' => $this->getCustomer()) );
        $this->_logout();
        Mage::dispatchEvent('after_customer_logout', array('customer' => $this->getCustomer()) );
    }
    return $this;
}

Unfortunately, M1 reaching is EOL, I highly doubt this will happen.

So the other solution is to observe the controller_action_postdispatch_customer_account_logout event instead of customer_logout

2
  • This just seems like a mis-use of stack overflow. You asked the question and answered it in the same minute. – Paras Sood May 18 '16 at 12:57
  • 2
    @ParasSood yep that's the purpose of the "answer my question" checkbox when creating a question. Regarding the use of stackoverflow you can find more details about answering your own question here: meta.stackexchange.com/questions/17845/… – Raphael at Digital Pianism May 18 '16 at 12:58

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.