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.

1
  • 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? Commented Oct 22, 2014 at 10:31

6 Answers 6

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.

1
  • 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
    Commented Oct 27, 2014 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

1
  • 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
    Commented Oct 23, 2014 at 13:54
2
/** @var Mage_Sales_Model_Quote $quote */
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->removeAllItems()->save();
1
  • Method 'removeAllItems' not found Commented Dec 20, 2017 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();
}
0

If you want to clear cart of a specific customer, here is the way

$customerId = 4;
$quote = Mage::getModel('sales/quote')->loadByCustomer($customerId);
$quote->removeAllItems()->save();
0

For Magento 2.3 Users And Above

For more modern versions of Magento it is now preferred to use Service Contract design patterns over Magento\Checkout\Model\Cart or Magento\Checkout\Helper\Cart therefore I was able to achieve the same functionality using Repositories as well as the Checkout Session manager:

/** @var \Magento\Quote\Model\Quote $cart */
$cart = $this->_checkoutSession
   ->clearQuote()
   ->getQuote()
   ->unsetData('any_custom_quote_attribute')
   ->removeAllItems();

/** @var \Magento\Quote\Api\CartRepositoryInterface $this->_cartRepository */
$this->_cartRepository->save($cart);

/** @var \Magento\Checkout\Model\Session $this->_checkoutSession */
$this->_checkoutSession->replaceQuote($cart);
3
  • This answer is for Magento 2, the question is about Magento 1 Commented Feb 8, 2022 at 6:43
  • Google doesn't think that way! Commented Feb 10, 2022 at 9:30
  • What does that even mean?! Commented Feb 10, 2022 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.