Magento - 如何检查产品是否已从购物车中删除

发布于 2024-10-04 07:19:20 字数 126 浏览 2 评论 0原文

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

购物车对象是否包含任何可以告诉我产品是否已从购物车中删除的内容?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-10-11 07:19:21

Magento 不会记录哪些项目已被删除,您必须自己做。首先监听一个事件;

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() 调用)来查看购物车中曾经有哪些产品。

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

?>

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.

紫轩蝶泪 2024-10-11 07:19:21

是的,您可以像这样获取购物车中的当前商品:-

foreach ($session->getQuote()->getAllItems() as $item) {

    $output .= $item->getSku() . "<br>";
    $output .= $item->getName() . "<br>";
    $output .= $item->getDescription() . "<br>";
    $output .= $item->getQty() . "<br>";
    $output .= $item->getBaseCalculationPrice() . "<br>";
    $output .= "<br>";
}

此链接可能有帮助
http://www.magentocommerce.com/boards/viewthread/19020/

yes you can get current items in cart like this:-

foreach ($session->getQuote()->getAllItems() as $item) {

    $output .= $item->getSku() . "<br>";
    $output .= $item->getName() . "<br>";
    $output .= $item->getDescription() . "<br>";
    $output .= $item->getQty() . "<br>";
    $output .= $item->getBaseCalculationPrice() . "<br>";
    $output .= "<br>";
}

This link may helpful
http://www.magentocommerce.com/boards/viewthread/19020/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文