客户登录后如何删除旧的购物车产品


8

客户访问网站并添加产品,

然后客户登录。

如果客户已经有购物车项目,则其删除的旧项目,新添加的项目仅显示

例:

客户已经在购物车中有5个产品>访问站点>在购物车中新添加2个产品>登录客户帐户>显示购物车新添加2个产品(旧购物车产品已删除)

任何建议将不胜感激。

Answers:


7

使用此事件sales_quote_merge_before

把它放在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> 

把它放在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; 
} 

引用此链接


我从链接中得到了答案。感谢@surya
VijayS91

3

我建议您挂接到名为的事件sales_quote_merge_before并清空购物车之一(例如现有的购物车)。
登录后和之前触发此事件sales_quote_collect_totals_before


这样就能实现想要的目标吗?还是没有完全干预您想要的位置。
朱利安·拉查
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.