How to clear/remove shopping cart items before adding new items in magento?

How to clear/remove shopping cart items before adding new items in magento?

July 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to clear/remove shopping cart items before adding new items in magento?

Clearing Magento Shopping Cart Items Before Adding New Products

Hello everyone! Today, we’re going to tackle a common issue that many Magento developers face - how to clear or remove all shopping cart items before adding new products. We’ll provide a step-by-step guide using the correct approach and address the confusion around the incorrect code snippets provided by the user.

Understanding the Problem

The main challenge lies in ensuring that all existing cart items are completely removed while preserving the integrity of the checkout process. The provided codes attempt to clear the cart but fail to handle the session state correctly, leading to issues when adding new products.

Let’s dive into each problematic code snippet:

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

    • This clears the entire session, including important data like login status and currency settings.
    • It doesn’t specifically target cart-related sessions.
  2. Mage::getSingleton('checkout/cart')->truncate();

    • This removes all items from the cart but doesn’t save the changes to the database.
    • The item count remains as it was, causing inconsistencies.
  3. $quote = Mage::getSingleton('checkout/session')->getQuote(); $quote->delete();

    • This deletes the quote entity but doesn’t update the session or clear the cart items in memory.
    • Items still remain in the cart until the next page refresh.
  4. Mage::getSingleton('checkout/cart')->truncate()->save();

    • Combining both actions solves the previous issues, as it removes all items and saves the changes to the database.
    • However, it doesn’t clear the session state, which can cause unexpected behavior during checkout.
  5. Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

    • Setting this flag is not necessary for clearing the cart and may confuse the system.

6-8 are variations of removing individual items or using helper methods, but they still face the same underlying problems:

The Correct Approach

To clear the shopping cart completely and prepare it for new items, follow these steps:

  1. First, create a new controller action to handle the cart clearing process separately from adding products.
public function clearCartAction()
{
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    // Remove all items
    $quote->removeAllItems();

    // Set flag to indicate cart has been updated (optional, for UI purposes)
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

    // Save the changes to the database
    $quote->save();

    // Redirect back to the cart page or any desired location
    $this->_redirect('checkout/cart');
}
  1. Call this action before adding new products to your cart.
$productId = 123; // Replace with your product ID
$cart = Mage::getModel('checkout/cart');

try {
    if ($productId) {
        $cart->addProduct($product, 1);
        $cart->save();
    }
} catch (Exception $e) {
    Mage::logException($e);
}
  1. If you need to clear the cart from a specific product page or during a custom event, create an observer:
<events>
    <event_name> // Replace with your event name (e.g., controller_action_predispatch_checkout_cart_index)
        <observers>
            <clear_cart_after_event>
                <type>singleton</type>
                <class>Namespace_Module_Model_Observer</class>
                <method>clearCartAfterEvent</method>
            </clear_cart_after_event>
        </observers>
    </event_name>
</events>
  1. In the observer, use the same logic as in step 1 to clear the cart and update the quote.

Conclusion

Clearing the Magento shopping cart before adding new items requires a careful approach that balances removing all existing data with preserving essential session information. By following the steps outlined above and avoiding the pitfalls of the provided code snippets, you can create a clean and efficient cart-clearing mechanism for your Magento store.

Remember to test thoroughly in different scenarios, including multiple browsers, devices, and user roles, to ensure that the new items are added correctly after clearing the cart.

If you found this guide helpful, please share it with your fellow developers and leave any questions or suggestions in the comments below. Thank you for reading!

Happy coding! 💻

Back to LaravelCompany Blog

Checkout Catalog Database Configuration

Our network