How To Clear / Delete Shopping Cart Items of Single or All Customers in Magento.?

Clear or Delete Shopping Cart Items of Single users in Magento:-

With the following code one can remove the cart items of the currently logged in single customers respectively:

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

Clear or Delete Shopping Cart Items of all customers in Magento:-

With this code one can remove all cart items for all users from Magento shopping cart accordingly:

$quoteCollection = Mage::getModel('sales/quote')
            ->getCollection()
            ->addFieldToFilter('is_active', 1);
                        
foreach ($quoteCollection as $item) {
    $item->delete();    
}

Large number of customer quotes can be time consuming and resources can hand the server so use the following SQL query that can delete all Magneto all customer cart items:

DELETE FROM sales_flat_quote WHERE is_active = 1;

is_active = 0 means those quotes have been converted into orders, i.e. customer has placed order for those quotes.
is_active = 1 means quotes that have not been ordered, i.e. quotes present in the shopping cart of customers


Running this query will automatically delete the related rows (quote ‘items’) from sales_flat_quote_item table through foreign key constraint accordingly.

You may also like...

Leave a Reply

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