magento shopping cart does not get clear

magento shopping cart does not get clear

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento shopping cart does not get clear

Clearing Magento Shopping Cart After Successful Checkout

Magento’s onepage checkout is a powerful feature that streamlines the purchasing process for your customers. However, one common issue developers face is how to clear the shopping cart after a successful order placement without redirecting the user back to the cart page prematurely.

Understanding the Current Scenario

In your case, you’re using a three-step checkout process with billing and shipping information merged. After placing an order successfully, you want to empty the shopping cart but avoid redirecting the customer back to the cart page directly from the order summary step.

The code snippet you provided clears the cart by iterating through all items and removing them:

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

While this works in terms of clearing the cart, it causes an immediate redirect to the cart page after each item is removed.

The Issue with savePaymentAction()

The savePaymentAction() in the OnepageController.php file is called after the payment information step. If you attempt to clear the cart here, Magento’s default redirect behavior will take over and send the user back to the cart page before they can see their order confirmation.

The Solution: Clearing the Cart After Order Placement

To fix this issue without causing an unwanted redirection, we need to modify the cart clearing process. Here’s a step-by-step approach:

  1. Prevent Automatic Redirection: In your savePaymentAction(), add a flag to prevent Magento’s default redirect behavior.
Mage::getSingleton('checkout/session')->setNoCartRedirect(true);

This tells Magento not to redirect the user back to the cart page after the order is placed successfully.

  1. Clear the Cart After Order Placement: Instead of clearing the cart in savePaymentAction(), move this logic to a separate observer that triggers after the order has been saved.

    Create an event observer for the checkout_type_onepage_save_order_after event:

<events>
    <checkout_type_onepage_save_order_after>
        <observers>
            <remove_items_from_cart>
                <type>singleton</type>
                <class>Namespace_Module_Model_Observer</class>
                <method>clearCartAfterOrderPlacement</method>
            </remove_items_from_cart>
        </observers>
    </checkout_type_onepage_save_order_after>
</events>

Then, implement the observer method:

public function clearCartAfterOrderPlacement($observer)
{
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    foreach ($quote->getItems() as $item) {
        $quote->removeItem($item->getId());
    }
    $quote->save();

    return $this;
}
  1. Ensure the Session is Cleared: To ensure that the shopping cart session is completely cleared, you can add a line to remove the quote from the session:
Mage::getSingleton('checkout/session')->clear();

Conclusion

By following this approach, you ensure that the Magento shopping cart gets cleared after a successful order placement without causing an unwanted redirection back to the cart page. This provides a better user experience by allowing customers to review their order before the cart is emptied.

Remember to replace Namespace_Module with the actual namespace of your module in the observer configuration.

For more advanced customizations and best practices in Magento development, consider exploring our Magento Development Services at Laravel Company.

Checkout Orders Customers Errors

Our network