How to delete shopping cart items for customers in magento

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

How to delete shopping cart items for customers in magento

How to delete shopping cart items for customers in magento

Here, We will explain How to delete shopping cart items for customers in magento?

The code for delete shopping cart items for logged in customer:

1
2
3
4
5
6
7
8
9
$cart = Mage::getSingleton('checkout/cart'); 
$quoteItems = Mage::getSingleton('checkout/session')
                  ->getQuote()
                  ->getItemsCollection();
 
foreach( $quoteItems as $item ){
    $cart->removeItem( $item->getId() );    
}
$cart->save();

The code for delete shopping cart items for all customers:

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

The query for delete shopping cart items for all customers using SQL:

1
DELETE FROM sales_flat_quote WHERE is_active = 1;

Keep in Mind:
– 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 related rows (quote items) from sales_flat_quote_item table through foreign key constraint.

Add comment


 

Pin It on Pinterest

Share This