How to delete a quote in Magento?

In: Feeling guru|Techie Daw

13 Apr 2009
Advertisement

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:

Advertisement

First, insert the Magento app snippets which can be found here

then use the code below to delete a quote:

<?php
$quoteID = Mage::getSingleton("checkout/session")->getQuote()->getId();

if($quoteID)
{
    try {
        $quote = Mage::getModel("sales/quote")->load($quoteID);
        $quote->setIsActive(false);
        $quote->delete();

        return "cart deleted";
    } catch(Exception $e) {
        return $e->getMessage();
    }
}else{
    return "no quote found";
}

About the author

Richard Feraro is a PHP Developer living in Manila, Philippines. He has been programming for nine (9) years and working on different open source application mostly done in PHP and MySQL.

10 Responses to How to delete a quote in Magento?

Avatar

Kris

April 22nd, 2010 at 1:20 am

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.

Avatar

Richard Feraro UNITED STATES

April 22nd, 2010 at 3:48 pm

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

Avatar

ruurdz NETHERLANDS

September 1st, 2010 at 6:36 pm

Hello Richard,
One minor bug:
getQuote is a function so it should use braces like this: getQuote()

Thanks for sharing this code.

Avatar

Richard Feraro UNITED STATES

September 1st, 2010 at 7:38 pm

Oops! Didn’t see that one. Thanks for the note :)

Avatar

Corey UNITED STATES

December 20th, 2010 at 2:21 pm

What files is the code placed in Magento? I really need to get the cart emptied.

Avatar

Kristy UNITED STATES

December 20th, 2010 at 2:42 pm

What Magento file do you place the above code in? I’m using 1.4.1.1

Avatar

Richard Feraro UNITED STATES

December 20th, 2010 at 4:33 pm

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 :)

Avatar

Richard Feraro UNITED STATES

December 20th, 2010 at 4:34 pm

Same as my reply above to Corey :)

Avatar

Jerome Dennis D INDIA

September 28th, 2011 at 3:50 pm

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;

Avatar

Richard Feraro MALAYSIA

September 29th, 2011 at 1:15 am

You’re welcome Jerome :)

Comment Form

About my blog

This is a programmer's blog. Whenever I encounter a difficult coding task, I make sure that I'll be able to share it here in the hope that others may find it useful. It's my way of giving back to the open source community who have been a great help to me as well.

Share This Post

or bookmark to your mobile

Bookmark: http://mysillypointofview.richardferaro.com/2009/04/13/how-to-delete-a-quote-in-magento/

Advertisement

WordPress + Magento

Proudly Pinoy!

Proudly Pinoy!

Recent Trackbacks

Archives

Disclaimer

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or my mom.

Viewers by Country

in India 3274 (21 %) 3781 (19 %)
us United States 2739 (17 %) 3560 (18 %)
gb United Kingdom 926 (6 %) 1176 (6 %)
de Germany 767 (5 %) 989 (5 %)
nl Netherlands 586 (4 %) 743 (4 %)
fr France 483 (3 %) 606 (3 %)
ph Philippines 468 (3 %) 763 (4 %)
br Brazil 443 (3 %) 547 (3 %)
au Australia 334 (2 %) 402 (2 %)
ca Canada 330 (2 %) 408 (2 %)
es Spain 323 (2 %) 429 (2 %)
it Italy 318 (2 %) 400 (2 %)
vn Vietnam 285 (2 %) 357 (2 %)
pk Pakistan 278 (2 %) 307 (2 %)
ro Romania 261 (2 %) 319 (2 %)
cn China 246 (2 %) 351 (2 %)
ua Ukraine 240 (2 %) 297 (2 %)
pl Poland 138 (1 %) 167 (1 %)
ru Russia 134 (1 %) 171 (1 %)
id Indonesia 132 (1 %) 159 (1 %)
Total Countries: 139
Total Visits: 15686
Total Pageviews: 19735