To clear the Magento cart programmatically, you can use these 3 methods.

First Method


$cart = Mage::getSingleton('checkout/cart');
$cart->truncate();
$cart->save();
Mage::getSingleton('checkout/session')->clear();

This code first gets an instance of the cart using the Mage::getSingleton('checkout/cart') method. Then, it uses the truncate method to remove all items from the cart. The save method is used to save the changes. Finally, the checkout session is cleared using Mage::getSingleton('checkout/session')->clear().

 

Second Method

<?php require_once 'app/Mage.php'; umask(0); Mage::app(); $cart = Mage::getSingleton('checkout/cart'); $cart->truncate();
$cart->save();
Mage::getSingleton('checkout/session')->clear();

This code will clear the current customer’s shopping cart and remove all items. The Mage::app() method initializes the Magento environment, and the Mage::getSingleton('checkout/cart') method retrieves the current customer’s shopping cart. The truncate method removes all items from the cart, and the save method updates the cart in the database. Finally, the Mage::getSingleton('checkout/session')->clear() method clears the checkout session.

 

Third Method


<?php  
require_once 'app/Mage.php';
Mage::app(); 
$quote = Mage::getSingleton('checkout/session')-&gt;getQuote(); 
$quote-&gt;removeAllItems(); $quote-&gt;save(); 
?>

This code retrieves the current quote object using Mage::getSingleton(‘checkout/session’)->getQuote() and then removes all items from the cart using removeAllItems(). Finally, the changes are saved using $quote->save().

You can place this code in a custom module or add it to the index.php file in your Magento installation. Note that this code assumes that the Magento application is already initialized, so you may need to adjust the code if you’re running it outside of the Magento environment.

It’s important to note that this code should be placed in a .php file and executed from the command line or a browser, not directly within the Magento environment.