In Magento how to make empty cart whenever customer logged in?

by Ashish Kacha   Last Updated April 16, 2018 07:09 AM

If you have any idea then please share here.

Tags : cart

Answers 2

On the Mage_Customer_Model_Session model's method setCustomerAsLoggedIn() an event customer_login is being dispatched. I guess you need to hook in on that event. You could do this as follows.

Add the event to your module's config.xml file (app/code/local/Lpf/ModuleCookie/etc/config.xml):

<?xml version="1.0"?>

Now create a model Lpf_ModuleCookie_Model_Observer (app/code/local/Lpf/ModuleCookie/Model/Observer.php). Add a customerLogin() method to the class:

 class Lpf_ModuleCookie_Model_Observer

  * Run couple of 'php' codes after customer logs in
  * @param Varien_Event_Observer $observer
 public function customerLogin($observer)
     foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
             Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();

Reference : How to bind an action after customer login success using Magento?

Chirag Rajput
Chirag Rajput
April 16, 2018 06:53 AM

Use this event sales_quote_merge_before

Put this in config.xml In below-mentioned code, we are using sales_quote_merge_before observer to clear old items.

   <sales_quote_merge_before><!--calling this event before merging the old cart with newly added cart items while login--> 
            <ws_clearoldcartproducts_observer><!--unique identifier name for our observer--> 
                <class>Ws_Clearoldcartproducts_Model_Observer</class><!--Our observer class name--> 
                <method>loadCustomerQuote</method><!--Method to be called from our observer class--> 

Put this in observer.php

public function loadCustomerQuote() 
    $customerQuote = Mage::getModel('sales/quote') 
    if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) 
        // Removing old cart items of the customer. 
        foreach ($customerQuote->getAllItems() as $item) 
            if ($item->getHasChildren()) { 
                foreach ($item->getChildren() as $child) { 
        $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer()) ->setTotalsCollectedFlag(false) ->collectTotals() ->save();
    return $this; 

Hope that'll help you.

Emipro Technologies Pvt. Ltd.
Emipro Technologies Pvt. Ltd.
April 16, 2018 07:00 AM

Related Questions

Missing mini cart on header

Updated June 11, 2015 01:04 AM

When press browser back button cart is empty

Updated July 11, 2018 21:09 PM

Adding Products to Shopping Cart

Updated May 14, 2018 12:09 PM