Categories: Magento

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.

Rohan pathak

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

12 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago