Return to Snippet

Revision: 36987
at December 2, 2010 05:46 by TioSolid


Initial Code
public function loadCustomerQuote()
    { 
        $lastQid = Mage::getSingleton('checkout/session')->getQuoteId(); //quote id during session before login;
        if ($lastQid) { //before login session exists means cart has items                            
            $customerQuote = Mage::getModel('sales/quote')
                ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId()); //the cart from last login         
            //set it to the session before login and remove its items if any
            $customerQuote->setQuoteId($lastQid);
            $this->_removeAllItems($customerQuote);
            
        } else { //no session before login, so empty the cart (current cart is the old cart)
            $quote = Mage::getModel('checkout/session')->getQuote();                                
            $this->_removeAllItems($quote);
        }        
    }
    
    /**
     * iterate through quote and remove all items
     *           
     * @return nothing
     */
    protected function _removeAllItems($quote){
        //reset all custom attributes in the quote object here, eg:     
       // $quote->setDestinationCity('');
        
        foreach ($quote->getAllItems() as $item) {
            $item->isDeleted(true);
            if ($item->getHasChildren()) foreach ($item->getChildren() as $child) $child->isDeleted(true);
        }
        $quote->collectTotals()->save();        
    } //_removeAllItems

Initial URL
http://www.magentocommerce.com/boards/viewthread/73612/

Initial Description
I’ve implemented something similar a couple of months ago. What we wanted was to prevent the cart from the old session from merging into the cart of the current session at the point when the customer login.  If that describes what you want as well, then the way I did it was to overwrite Mage_Checkout_Model_Observer.  The function loadCustomerQuote() in the observer observes the event customer_login and merges the current session quote with the last login quote object

Initial Title
Dont merge carts after client login

Initial Tags
magento

Initial Language
PHP