8

Customer visit website and add the product,

then customer login.

if customer already have cart items, its removed old items, newly added items only showing

Example:

Customer already have 5 products in cart > Visit the site > add newly 2 products in cart > login customer account > show the cart newly add 2 product (old cart products are removed)

Any suggestions will be appreciated.

7

use this event sales_quote_merge_before

put this in config.xml

<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> 

put this in observer.php

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; 
} 

Refer this link

  • i got the answer from the link. thanks @surya – VijayS91 May 15 '14 at 13:28
3

I suggest you hook onto the event called sales_quote_merge_before and empty one of the cart (the existing one for example).
This event is triggered after login and before sales_quote_collect_totals_before

  • were you able to achieve what you wanted this way? or does it not quite intervene where you'd like it to. – Julien Lachal May 14 '14 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.