I am in situation where I have to remove all products from cart after user logs in.
I have seen code examples to remove previous cart items, but don't know how to make the cart completely empty after the user logs in. How to remove the old cart product after customer login
The above url is for the logic Customer already have 5 products in cart> Visit the site> add newly 2 products in cart> login customer account> show the cart newly add 2 product (old cart products are removed)
But in my case, When the customer logs in all 7 products has to be removed from cart.
Any suggestions will be appreciated. Thank you.
-
Does this help you? blog.magepsycho.com/…MagePsycho– MagePsycho2014年12月13日 10:03:59 +00:00Commented Dec 13, 2014 at 10:03
1 Answer 1
Use an event observer to empty cart on customer login.
config.xml
<customer_login>
<observers>
<foo_bar> <!-- foo_bar should be unique tag for within all customer_login observers-->
<class>foo_bar/observer</class>
<method>customerLogin</method>
<type>singleton</type>
</foo_bar>
</observers>
</customer_login>
Observer
class Foo_Bar_Model_Observer {
public function customerLogin(Varien_Event_Observer $observer) {
foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) {
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}
}
}
-
1Or you can directly call
Mage::getSingleton('checkout/cart')->truncate()->save();in the observer.michael– michael2014年12月13日 19:18:13 +00:00Commented Dec 13, 2014 at 19:18