In Magento 1.9.3.3 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"?>
<config>
<modules>
    <Lpf_ModuleCookie>
        <version>0.1</version>
    </Lpf_ModuleCookie>
</modules>
<global>
    <models>
        <lpf_modulecookie>
            <class>Lpf_ModuleCookie_Model</class>
        </lpf_modulecookie>
    </models>
</global>
<frontend>
    <events>
        <customer_login>
            <observers>
                <lpf_modulecookie_customer_login>
                    <type>model</type>
                    <class>lpf_modulecookie/observer</class>
                    <method>customerLogin</method>
                </lpf_modulecookie_customer_login>
            </observers>
        </customer_login>
    </events>
</frontend>

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

 <?php
 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.

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

Put this in observer.php

public function loadCustomerQuote() 
{ 
    $customerQuote = Mage::getModel('sales/quote') 
                        ->setStoreId(Mage::app()->getStore()->getId())
                        ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId()
                    ); 
    if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) 
    { 
        // Removing old cart items of the customer. 
        foreach ($customerQuote->getAllItems() as $item) 
        { 
            $item->isDeleted(true); 
            if ($item->getHasChildren()) { 
                foreach ($item->getChildren() as $child) { 
                    $child->isDeleted(true); 
                } 
            } 
        } 
        $customerQuote->collectTotals()->save(); 
    } 
    else 
    { 
        $this->getQuote()->getBillingAddress(); 
        $this->getQuote()->getShippingAddress(); 
        $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