Clear abandoned cart items during login in Magento

You may have noticed that old cart items (abandoned) get merged with the current one when the customer tries to log in.
It’s good in terms of sales but sometimes your merchant simply may not want this feature.
In this case, we can easily clear the abandoned cart items and stop being merged with the current cart with the following steps:

Step 1. Hooking into the event: load_customer_quote_before
Add the following code in the file app/code/local/MagePsycho/Module/etc/config.xml

<frontend>
	<events>
		<load_customer_quote_before>
			<observers>
				<module_load_customer_quote_before>
					<class>module/observer</class>
					<method>clearAbandonedCarts</method>
				</module_load_customer_quote_before>
			</observers>
		</load_customer_quote_before>
	</events>
</frontend>

Step 2. Implementing logic in observer method
Add the following code in the file app/code/local/MagePsycho/Module/Model/Observer.php

<?php
class MagePsycho_Module_Model_Observer
{
	public function clearAbandonedCarts(Varien_Event_Observer $observer)
	{
		$lastQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
        if ($lastQuoteId) {
            $customerQuote = Mage::getModel('sales/quote')
                ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
            $customerQuote->setQuoteId($lastQuoteId);			
            $this->_removeAllItems($customerQuote);

        } else {
            $quote = Mage::getModel('checkout/session')->getQuote();
            $this->_removeAllItems($quote);
        }
	}

	protected function _removeAllItems($quote){
        foreach ($quote->getAllItems() as $item) {
            $item->isDeleted(true);
            if ($item->getHasChildren()) {
				foreach ($item->getChildren() as $child) {
					$child->isDeleted(true);
				}
			}
        }
        $quote->collectTotals()->save();
    }
}

That’s it.

[EDIT]
Now you can download this trick as a Magento Module from [here]

Happy Coding!