1

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.

asked Dec 13, 2014 at 9:58
1

1 Answer 1

2

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();
 }
 }
}
answered Dec 13, 2014 at 15:36
1
  • 1
    Or you can directly call Mage::getSingleton('checkout/cart')->truncate()->save(); in the observer. Commented Dec 13, 2014 at 19:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.