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.

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

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.