search
top

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 Responses to “Empty Cart in Magento”

  1. Igor says:

    Maybe you should must save $checkout_cart model after foreach loop?

  2. Anton says:

    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

    • Pragnesh says:

      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

top