Clear existing items in cart before adding a new Item in Magento

Clear existing items in cart before adding a new Item in Magento

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Clear existing items in cart before adding a new Item in Magento

Clearing Existing Items in Magento Cart Before Adding New Ones

When building an e-commerce platform using Magento, you might face situations where you need to clear all existing items from the cart whenever a new product is added. This is particularly useful when implementing certain promotional rules or single-product promotions.

Understanding the Current Issue

Let’s break down the current problem described:

  • The addAction() in CartController.php needs to be overridden.
  • Before adding a new item, all existing items need to be cleared from the cart.
  • The provided code attempts this but clears the entire cart when a new item is added.

Why the Current Approach Isn’t Working

The issue lies in the approach of removing items directly within the addAction(). When you loop through the current items and remove them using $checkout_my_cart->removeItem($itemId)->save(), Magento interprets this as a series of separate actions, leading to the entire cart being emptied.

The Correct Approach: A Two-Step Process

To solve this issue correctly, follow these steps:

Step 1: Retrieve the Current Cart

Use the following code to retrieve the current cart items:

$checkout_my_cart = Mage::getSingleton('checkout/cart');
$current_items = $checkout_my_cart->getItems();

This retrieves all the currently existing items in the cart.

Step 2: Clear All Items Using a Single Action

Instead of removing items one by one, use Magento’s built-in truncate() method to clear all items at once:

$checkout_my_cart->truncate()->save();

The truncate() function empties the entire cart in a single database operation. After calling this, your cart will be completely empty.

Implementing It in the addAction()

Now, integrate these steps into your addAction() method:

public function addAction()
{
    $checkout_my_cart = Mage::getSingleton('checkout/cart');
    $current_items = $checkout_my_cart->getItems();
    foreach ($current_items as $item) {
        $itemId = $item->getItemId();
        // No need to remove items individually; use Magento's built-in function
    }

    // After the foreach loop, there's no need for any additional code

    $product_id = (int) $this->getRequest()->getParam('product');
    if ($product_id > 0 && Mage::registry('product')) {
        $product = Mage::registry('product');

        try {
            $request = $this->getRequest();
            $params = array(
                'qty' => $request->getParam('qty', 1)
            );
            $cart->addProduct($product, $params);

            // Save the cart
            $cart->save();

            $this->_goBack();

        } catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addError($e->getMessage());
            $this->_redirect('checkout/cart');
        }
    }

    return;
}

Conclusion

By using Magento’s built-in truncate() function, you can effectively clear all existing items from the cart in a single database operation before adding your new item. This approach eliminates the need for looping through and removing each item individually.

Remember, this method should be used judiciously. Clearing the entire cart with every addition will remove any previously added products, which might not always be the desired behavior. Ensure it aligns with your specific business requirements.

For more advanced Magento customizations and best practices, consider exploring the resources available at Laravel Company. They provide comprehensive guides on Magento development and e-commerce solutions tailored to your needs.

Don’t hesitate to reach out if you have any further questions or need clarification on this implementation. Happy coding!

Checkout Catalog Errors

Our network