Magento:当用户登录用户"添加到购物车"时,不应将该产品添加到用户配置文件中

Tha*_*ham 1 php magento

我使用了Magento 1.9,我有这样的场景:

  1. 用户登录该站点,并"添加到购物车"产品A.
  2. 用户不支付产品A的费用,并关闭浏览器.
  3. 几天后,用户"添加到购物车"产品B,并在结帐时登录该网站
  4. 他/她的总数现在显示产品A和产品B.

我不喜欢这个工作流程.有没有办法,我可以在magento中更改此行为,当登录用户"添加到购物车"产品时,它不应该将该产品添加到用户配置文件.非常感谢你

Ela*_*san 6

Magento存储所有报价sales_flat_quotesales_flat_quote_item.第entity_id一个表将在第二个表中使用.报价将分别保存在此表中,其中包含客户详细信息和产品详细信息.如果客户未登录,则客户详细信息(customer_id,email,group等)将为空.但客户登录后,magento尝试添加客户详细信息,如果他/她有报价.这是默认的magento的行为.

但我们可以用简单的逻辑改变这种行为.也就是说,只要用户登录,我们就会将他当前的购物车报价详细信息与该用户以前的购物车匹配(如果存在).所以客户不会将他以前的购物车保存在magento中.(这就是你想要的......?).

例如,客户A在他的报价中有2个产品,保存在他之前的会话中.当他来到你的商店并尝试在他的购物车中添加3个产品作为客人.所以最初购物车只显示3种产品.因此,在他登录后,我们将删除他之前的购物车细节并添加当前的购物车细节.因此,顾客只会拥有他当前的购物车物品并且不知道他以前的购物车物品.

为实现这一点,我们有两种方法

  1. 观察customer_login并将客户当前购物车替换为他之前的购物车. 要么
  2. 覆盖Mage_Checkout_Model_Observer类.

我认为1'是最好的方式.所以我只是在这里添加观察者,你的config.xml应该是这样的,

<?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> 
Run Code Online (Sandbox Code Playgroud)

Packagename/ModuleName/Model/Observer.php应该是

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

}
Run Code Online (Sandbox Code Playgroud)

而已.如果您有任何疑问,请在此处发表评论.

注意:

这里我singleton因为处理会话而使用了方法.