Empty the cart programatically in Magento

One of the last things I had to implement in Magento was a function to empty the customer cart. At first I found this great extension made by the Inchoo people, but it did much more than I needed. Having a controller to do this is fair enough, and the only code you need to put in that controller is the following:

$quote = Mage::getSingleton('checkout/session')->getQuote();

$quote->delete();

3 comments

  1. Reply

    I had this query a few days ago but I found one plugin. Thanks for this article. I was looking for it.

    • doh on January 29, 2013 at 11:45 am

    Reply

    This gives me:
    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`sales_flat_quote_item`, CONSTRAINT `FK_SALES_FLAT_QUOTE_ITEM_QUOTE_ID_SALES_FLAT_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `sales_flat_quote` (`entity_id`) ON DELETE CASC)

      • Pau on January 29, 2013 at 12:21 pm
      • Author

      Reply

      Hi doh,

      I’m not sure why that constraint is affecting. It shouldn’t. Does this happen to you for all the quotes or only for a particular one?

Leave a Reply

Your email address will not be published.