6

TL;DR When a customer is logged in, Mage::getSingleton('checkout/session')->clear(); doesn't clear their cart. I need something that will.


I've asked this as part of another question, but i discovered it was actually two problems, other other one now fixed.

I'm programatically creating an order in a controller using the items in the cart. After the order is saved the cart should be cleared. I do this using

Mage::getSingleton('checkout/session')->clear();

which works fine most of the time. The problem comes when the customer is logged-in. If they are logged in the cart doesn't clear. I've found other solutions (like load the cart items and removed them one at a time in a loop) but they don't meet all my requirements. WHat I need is something that will:

  1. remove all the items (obviously)
  2. stay on the same page (why loading the cart doesn't seem to work. I end up redirected to the cart page)
  3. NOT go to another page and then come back (2 redirects won't work in this case)

Is there something different I need to do when they are logged in? How can I get the cart cleared? Thanks all for the help.

  • If you create an order programmatically following the normal Magento process, the cart should I think end up empty automatically. What steps are you taking to create the order? – Jonathan Hussey Oct 22 '14 at 10:31
7

Check quote status is_active for the quote (sales_flat_quote) regarding to the created order. If its active (value is 1) set it inactive ($quote->setIsActive(0)->save()) after successfully order creation and than clear checkout session.

  • This seems to be doing it. The process it follows leaves magento before the quote is made inactive, then comes back to create the order after that would have happened. This seems to make it work just fine. – Matt Flamm Oct 27 '14 at 13:02
4
This is against standard Magento logic, so you need a custom module that will observe customer_logout event and execute the following code bit:

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
 Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}

more info

  • I'm not trying to clear it at log-out, rather to clear it after the order is made while still on the success screen – Matt Flamm Oct 23 '14 at 13:54
2
/** @var Mage_Sales_Model_Quote $quote */
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->removeAllItems()->save();
  • Method 'removeAllItems' not found – TheKitMurkit Dec 20 '17 at 21:49
0

This should do a job (note that without checking if cart actually has items it will not work good):

$cart = Mage::getSingleton('checkout/cart');
if (count($cart->getItems())) {
    $cart->truncate();
    $cart->save();
}

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.