Reputation: 38695
I used Magento 1.9 and I have this scenario:
I dont like this work flow. Is there a way, that I can change this behavior in magento, where when a logged in user "Add To Cart" a product, it should not add that product to the user profile. Thank you very much
Upvotes: 1
Views: 2733
Reputation: 2669
Magento stores all quotes in sales_flat_quote
and sales_flat_quote_item
. The entity_id
of first table will be used in second table. The quotes are saved in this tables with customer details and product details respectively. If customer is not in logged-in then the customer details (customer_id, email, group etc)will be empty. But customer once logged-in then magento tries add customer details those tables if quotes he/she have. This is default magento's behavior.
But we can change this behavior with simple logic. That is whenever user logged-in we will match his current cart-quote details with previous carts of that user if that exists. So customer will not have his previous cart that saved in magento.( That's what you want right ..?).
For example customer A having 2 products in his quote that saved in his previous session. When he come to your store and trying add 3 more products in his cart as a guest. So initially the cart shows only 3 products. So here after he logged-in we will delete his previous cart detail and add current one. So the customer will only have his current cart items and having no idea about his previous one.
To implement this, we have 2 ways
Mage_Checkout_Model_Observer
class.I think 1'st is the best way. So I just add observer here, your config.xml should be like this,
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
<global>
<models>
<modulename>
<class>Packagename_ModuleName_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<events>
<customer_login> <!-- identifier of the event we want to catch -->
<observers>
<customer_login_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>modulename/observer</class> <!-- observers class alias -->
<method>clearQuote</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</customer_login_handler>
</observers>
</customer_login>
</events>
</global>
</config>
and Packagename/ModuleName/Model/Observer.php should be
<?php
class Packagename_ModuleName_Model_Observer
{
public function clearQuote(Varien_Event_Observer $observer)
{
$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);
}
}
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();
} //_removeAllItems
}
That's it. If you have any doubts please comment here.
Note:
Here I have used singleton
method because of handling session.
Upvotes: 6