Empty Cart in Magento

In one of my project, I needed to explicity delete the items from cart after order placed.

You can also use this script for making a functionlity for giving an option for empting the cart at once from cart page.

Here is the script for the same. Place it in function ‘successAction’ in app\code\local\Mage\Checkout\controllers\OnepageController.php (copy this file from app\code\core\Mage\Checkout\controllers\OnepageController.php)


	/**
     * Order success action
     */
    public function successAction()
    {
		//below code for removing the items from cart
        //start
        $checkout_cart = Mage::getSingleton('checkout/cart');
    	$items = $checkout_cart->getItems(); 
        //Loop through all of cart items
        foreach ($items as $item) 
        {
            $itemId = $item->getItemId();            
            //Remove items, one by one
            try 
			{
	        	$checkout_cart->removeItem($itemId)->save();
	        }catch (Exception $e) {
	        	echo $this->__('Cannot remove the item.');
	   		}
        }	
        //end
     }


4 thoughts on “Empty Cart in Magento

  1. you are doing it wrong 🙂 by magento process payment method is responsible for deactivating the quote if successful payment is processed so the user has a new quote and can make another order.

    this is done by setting isActive(false); on quote object as quote is still saved in the database but inactivated

    1. In one of my project (Magento 1.5), I needed to delete cart items after order was placed, as they were not deleted by default, and there is only an option in admin end for restricting the days for quote.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top