Dont want to merge old carts items with current quote

Hi,guys
Here i explain how you can prevent the cart from the old session from merging
into the cart of the current session at the point when the customer login.
Problem is, that a customer could basically be done shopping, logs in to their
account and their cart gets filled with their old “last logged in” cart items as
well as the ones they just recently picked.
To prevent this from happening, here’s what I did
The function loadCustomerQuote() in the observer observes the event customer_login
and merges the current session quote with the last login quote object.
you can see  loadCustomerQuote() in :
Mage_Checkout_Model_Observer.
Mage_Checkout_Model_Session.


public function loadCustomerQuote()
{
$customerQuote = Mage::getModel('sales/quote')
         ->setStoreId(Mage::app()->getStore()->getId())
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());  

if ($this->getQuoteId() != $customerQuote->getId())
{
$this->_removeAllItems($customerQuote);    //this will remove all items

/*  if ($this->getQuoteId())  // put this on comment, 
it will merge the old carts items with new carts
{
$customerQuote->merge($this->getQuote())
->collectTotals()
->save();
}
$this->setQuoteId($customerQuote->getId());
if ($this->_quote) {
$this->_quote->delete();
}
$this->_quote = $customerQuote;*/
}
return $this;
}
/**
* iterate through quote and remove all items
*
* @return nothing
*/
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(); 
 
this is ready, go for it to crack this
Gaurav Mehta
 

Comments

  1. Unfortunately this isn't correct anymore in Magento 1.6. The code has changed...
    What I did was the following:


    $customerQuote = Mage::getModel('sales/quote')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());

    if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {

    /* PREVENT THE FOLLOWING FROM HAPPENING TO NOT LOAD CART


    if ($this->getQuoteId()) {
    $customerQuote->merge($this->getQuote())
    ->collectTotals()
    ->save();
    }

    $this->setQuoteId($customerQuote->getId());

    if ($this->_quote) {
    $this->_quote->delete();
    }
    $this->_quote = $customerQuote;
    } else {
    */

    $this->getQuote()->getBillingAddress();
    $this->getQuote()->getShippingAddress();

    etc
    etc
    etc

    ReplyDelete
  2. Yeah, above code is for the 1.4 version,
    Magento should upgrade the versions, obviously, they can change the code and functionality for better result..

    ReplyDelete
  3. Also you can put it in event "load_customer_quote_before", so you don't have to edit magento core

    ReplyDelete
  4. Yeah, true!
    Thanks pavel for your input.

    ReplyDelete

Post a Comment

Popular posts from this blog

Magento Reindexing problem - take too long time

Magento Navigation menu of CMS pages

Magento Admin login problem