How to stop Magento shopping carts merging on customer login

8 May 2015| Post by Graham

AuthorGraham

An annoying feature of Magento in my opinion is when you visit the website; add things to your cart whilst logged in, but for some reason don’t checkout.

If you were to visit the website again as a guest, add items to your cart and then login to checkout; you will notice all the items you previously added to the cart are all of a sudden re-added alongside your current items. This could often duplicate cart items, potentially annoying the customer.

The Fix

I created a custom module that stops the shopping carts from merging next time a customer logs in to the website.

Step 1 – Create The Module

The files you will need to create as follows :

{{app-dir}}/app/etc/modules/My_StopCartMerge.xml

				




true
local



This file let’s Magento know to include the “StopCartMerge” module during the module initialisation.

{{app-dir}}/app/code/local/My/StopCartMerge/etc/config.xml

				




0.0.1





My_StopCartMerge_Model






singleton
stopcartmerge/observer
stopCartMerge






The module config file registers an observer that listens to the “sales_quote_merge_before” event. This will allow us to tap into the process before the cart items are merged.

{{app-dir}}/app/code/local/My/StopCartMerge/Model/Observer.php

				
getSource()->hasItems()) {
if (is_object($observer->getQuote()) && $observer->getQuote()->getId()) {
$observer->getQuote()->removeAllItems();
}
}
return $this;
}
}

The first line of this file checks to see if the user has any items in the cart; If they do, it then checks to see if there are any items due to be merged into the basket that have been remembered from a previous session.

If there are items from the previous session then they are all cleared leaving only the items from the current session. The beauty of this is that the previous sessions are only cleared if the user has no items in their current session.

Conclusion

This is a quick and relatively lightweight solution that will doesn’t require the editing / rewriting of any Magento core files.

Be sure to refresh your cache before testing.

Scroll