How to Avoid Merging Old Cart Items in Magento?

This post will illustrate how to avoid merging old cart items to the current checkout session while customers are shopping in your Magento website.

A Quick Introduction:

Does your old cart items get merged with your current list?

Yeah! This will happen when users login to store and add some products into cart and leave the store without purchasing. After a day or 2 they might come back to the store and add some more products into cart without logging in and proceed to checkout. In checkout page they will be requested to login, after login they might see some additional products have been added into the cart which would have been the ones that they added in their previous visit.

In this case what we need to do is we should automatically clear old cart items and show the current session items to the customers.

Also Read: Turn Up Your Abandoned Carts into Sales

Below written are codes that are supported with explanation.

File: app/ect/modules/Ws_Clearoldcartproducts.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Ws_Clearoldcartproducts>
            <active>true</active>
            <codePool>local</codePool>
        </Ws_Clearoldcartproducts>
    </modules>
</config>

File: app/code/local/Ws/Clearoldcartproducts/etc/config.xml

In below mentioned code we are using sales_quote_merge_before observer to clear old items. To know about observer go through this link.

<?xml version = "1.0" ?>
<config>
    <modules>
        <Ws_Clearoldcartproducts>
            <version>1.0.0</version>
        </Ws_Clearoldcartproducts>
    </modules>
    <global>
        <events>
            <sales_quote_merge_before><!--calling this event before merging the old cart with newly added cart items while login-->
                <observers>
                    <ws_clearoldcartproducts_observer><!--unique identifier name for our observer-->
                        <type>singleton</type>
                        <class>Ws_Clearoldcartproducts_Model_Observer</class><!--Our observer class name-->
                        <method>loadCustomerQuote</method><!--Method to be called from our observer class-->
                    </ws_clearoldcartproducts_observer>
                </observers>
            </sales_quote_merge_before>
        </events>
    </global>
</config>

File: app/code/local/Ws/Clearoldcartproducts/Model/Observer.php

We have declared our observer class and action in above xml file. Below mentioned code is where our magic works!

<?php

class Ws_Clearoldcartproducts_Model_Observer extends Mage_Checkout_Model_Session {

    /**
     * Clears old cart items after login
     * 
     * @return object currently added cart items 
     */
    public function loadCustomerQuote() {
        $customerQuote = Mage::getModel('sales/quote')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());

        if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
            // Removing old cart items of the customer.
            foreach ($customerQuote->getAllItems() as $item) {
                $item->isDeleted(true);
                if ($item->getHasChildren()) {
                    foreach ($item->getChildren() as $child) {
                        $child->isDeleted(true);
                    }
                }
            }
            $customerQuote->collectTotals()->save();
        } else {

            $this->getQuote()->getBillingAddress();
            $this->getQuote()->getShippingAddress();
            $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
                    ->setTotalsCollectedFlag(false)
                    ->collectTotals()
                    ->save();
        }
        return $this;
    }

}
?>

What we have done in above code is we have overridden the Mage_Checkout_Model_Session::loadCustomerQuote() action to avoid merging of old cart items. Instead of merging the products we have just deleted it! That’s what we have done!

Try this out and share your feedback with us. If you have any other ideas, please share with us.

Related posts

7 thoughts on “How to Avoid Merging Old Cart Items in Magento?

  1. John

    Thanks for the explanation you provided with coding. It’s a great info for me to prevent merging of old cart items.

  2. Alba Fredic

    I’ve implemented the class in the given structure and fired the xml file to work out. After over riding in merging old cart items into new one is better for the current check out process.

  3. Miston

    You made some really good points there. I looked on the over all performance for avoiding cart abandonment and learned more about the issue
    and found most individuals will go along with this on this web site to merge customers.

  4. Francis Kim

    Is this available as a download somewhere? That would be really convenient.

    • Ramanathan

      Dear Francis,

      I guess it is not available for download because one part of the code is based on configuration and other based on the observer. So we need to add it separately.

  5. Leandro Cosas

    Great job man!

    Thanks a lot for sharing!

    • Ramanathan

      Hi Leandro,

      Good to hear from you, stay tuned for more updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">