Magento-如何检查产品是否已经从购物车中移除 [英] Magento - How to check if a product has already been removed from the cart

查看:69
本文介绍了Magento-如何检查产品是否已经从购物车中移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小模块,该模块将自动将产品添加到购物车(基于某些条件).但是,如果用户随后从购物车中删除了该自动产品,我需要知道,这样我就不会在当前会话中再次添加该产品.

I'm writing a small module that will add a product to the cart automatically (based on certain criterias). However if the user subsequently removes that automatic product from the cart I need to know so that I don't add it again in the current session.

购物车对象中是否有任何东西可以告诉我是否已经从购物车中移除了产品?

Does the cart object hold anything that can tell me if a product has been removed from the cart already?

推荐答案

Magento不会保留已删除哪些项目的记录,您必须自己做.首先听一个事件;

Magento doesn't keep record of which items have been removed, you will have to do that yourself. Start by listening for an event;

app/local/YOURMODULE/etc/config.xml

<config>
...
    <frontend>
        <events>
            <sales_quote_remove_item>
                <observers>
                    <class>YOURMODULE/observer</class>
                    <method>removeQuoteItem</method>
                </observers>
            </sales_quote_remove_item>
        </events>
    </frontend>
...

app/local/YOURMODULE/Model/Observer.php

<?php

class YOU_YOURMODULE_Model_Observer
{
    public function removeQuoteItem(Varien_Event_Observer $observer)
    {
        $product = $observer->getQuoteItem()->getProduct();
        // Store `$product->getId()` in a session variable
    }
}

?>

创建一个扩展Mage_Core_Model_Session_Abstract的会话类,并将其用于存储您在上述观察器中收集的产品ID.然后,您可以引用该会话对象(由Mage::getSingleton()调用)以查看购物车中曾经使用过哪些产品.

Create a session class that extends Mage_Core_Model_Session_Abstract and use it to store the product IDs you collect in the above observer. You can then refer to that session object (called by Mage::getSingleton()) to see what products used to be in the cart.

这篇关于Magento-如何检查产品是否已经从购物车中移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆