If you’re using an external site with Magento as a backend for e-commerce functionality, you will notice that the shopping cart items doesn’t get deleted after you checkout. Actually in Magento, a shopping cart is a quote. So to remove everything in the shopping cart, you must delete the quote currently used by the user. To do this, follow the code below:
First, insert the Magento app snippets which can be found here
then use the code below to delete a quote:
02 | $quoteID = Mage::getSingleton( "checkout/session" )->getQuote()->getId(); |
07 | $quote = Mage::getModel( "sales/quote" )->load( $quoteID ); |
08 | $quote ->setIsActive(false); |
11 | return "cart deleted" ; |
12 | } catch(Exception $e ) { |
13 | return $e ->getMessage(); |
16 | return "no quote found" ; |
About the author
Richard Feraro is a Magento Enterprise Certified developer from Manila, Philippines with 14 years of solid open-source development experience using Linux, Apache, MySQL & PHP.
Like this:
Like Loading...
Related
Is there anyway to just make a simple button on top that says “Empty Cart” that will empty the cart once clicked?
I know to get to those links (on the top right) you need to go to app/code/core/Mage/Checkout/Block/Links.php but I am struggling finding a solution that works in v1.4.
Hello Kris!
Someone named Peaker did the same thing in my WordPress using Magento session article. Check it out here
I hope it will help you get started with your programming task.
Regards
Hello Richard,
One minor bug:
getQuote is a function so it should use braces like this: getQuote()
Thanks for sharing this code.
Oops! Didn’t see that one. Thanks for the note
What files is the code placed in Magento? I really need to get the cart emptied.
Hi Corey, the code above assumes that you’re using Magento’s Mage.php file to an external site, so it could be any file actually
What Magento file do you place the above code in? I’m using 1.4.1.1
Same as my reply above to Corey
Thanks Richard.
This is the perfect and simple solution to empty the cart.
Instead of doing like this given below
foreach ($this->_getQuote()->getAllItems() as $item) :
$this->_getCart()->removeItem($item->getId())->save();
endforeach;
You’re welcome Jerome
My shopping cart items doesn’t get deleted after you checkout and I’m not using an external site. This only happens when there is a registered user, but when a guest places and order the items get deleted. Thanks For the help
How can I get quote items of a customer and delete it?
How to get quote id on magento backend. I need all quoted items / product list at the time of order creation from magento backend
Works great bro…..
Nice article. It was very helpfull.