【问题标题】:How to prevent Magento removing grouped items如何防止 Magento 删除分组项目
【发布时间】:2013-09-24 09:08:08
【问题描述】:

我已经成功创建了一个包含 4 个产品的分组产品,并且一切正常。但是,其中一个项目是免费项目,仅在购买组合产品时可用。我的问题是,去购物篮时,我可以编辑它并删除一些项目。如果有人从购物篮中编辑分组产品并抛出消息,有没有办法删除免费项目,这可能吗?

我正在使用 Magento v1.3.2.4

更新:

我仍然有问题!根据 Marius 的建议,我创建了一个名为 FreePins 的自定义模块,并在 app/etc/modules/ 中使用以下代码

<?xml version="1.0"?>
<config>
    <modules>
        <test_FreePins>
            <active>true</active>
            <codePool>local</codePool>
        </test_FreePins>
    </modules>
</config>

我在 app/code/local/test/FreePins/etc/config.xml 中创建并添加了以下内容

    <?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <test_FreePins>
            <version>0.1.0</version>
        </test_FreePins>
    </modules>
    <global>
    </global>
    <frontend>
        <events>
                <sales_quote_remove_item>
                    <observers>
                        <test_FreePins>
                                <class>test_FreePins/observer</class>
                                <method>removeFreeItems</method>
                        </test_FreePins>
                    </observers>
                </sales_quote_remove_item>
        </events>
    </frontend>
</config>

最后,我在 app/code/local/test/FreePins/Model/Observer.php 的 Observer 类中有以下内容

<?php

class test_FreePins {

    public function removeFreeItems($observer) {
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $productId = $quoteItem->getProductId();

        print_r($productId);

        if($productId != 238 || $productId != 22 || $productId != 4) {
            return $this;
        }
    }

}

?>

我不完全确定这是否正确,因为添加后我无法从购物篮中移除物品。如果我注释掉模块配置中的 Frontend 标签,网站可以工作,但我的功能没有运行,谁能帮忙?

【问题讨论】:

    标签: php magento observers


    【解决方案1】:

    您可以为事件sales_quote_remove_item 创建一个观察者。在该检查中,删除的项目是否是分组产品的一部分。如果是,请同时删除免费产品。
    像这样的东西(用你的模块名称替换[module]): 在您的模块的config.xml 中,将其添加到&lt;frontend&gt; 标记内。

    <events>
        <sales_quote_remove_item>
           <observers>
               <[module]>
                   <class>[module]/observer</class>
                       <method>removeFreeItems</method>
               </[module]
           </observers>
        </sales_quote_remove_item>
    </events>
    

    在你的观察者类中添加这个方法:

    public function removeFreeItems($observer){
       $quoteItem = $observer->getEvent()->getQuoteItem();
       $productId = $quoteItem->getProductId();
       if (the $productId is not part of the grouped product){//add logic here
            return $this;//stop here
       }
       foreach ($quoteItem->getQuote()->getAllItems() as $item){
           if ($item is free){//add your logic here
               $item->isDeleted(true);
           }
        }
    }
    

    【讨论】:

    • 感谢您的回复马吕斯。我不完全确定如何制作一个模块,但我找到了 this ,所以一旦我设置好它,我会尝试你的代码并回发
    【解决方案2】:

    您可以使用“购物车价格规则”来做到这一点。但是,如果您使用这种方法,商品将在购物车中显示为全价,并且将应用折扣。如果你能忍受,这里是如何做到的:

    1. 由于您无法在产品 ID 上传递规则,我们需要创建一个新的隐藏类别(导航中未使用或停用的类别),您可以在其中添加捆绑产品
    2. 创建另一个隐藏类别,您可以在其中添加应该免费的项目
    3. 创建一个没有优惠券和高优先级(0 为最高)的新“购物车价格规则”
    4. 作为条件添加“产品属性组合”,然后选择“产品属性->类别”
    5. 作为类别,将先前创建的类别与您的捆绑包一起使用
    6. 在“操作”选项卡上,选择“产品价格折扣百分比”并将折扣金额设置为“100”
    7. 在“仅将规则应用于符合以下条件的购物车项目(所有项目留空)”下的同一选项卡上,再次选择“产品属性组合”,然后选择“产品属性->类别”,但现在选择类别与“免费物品”。
    8. 一旦您将购物车捆绑在一起,您就完成了,将应用并显示折扣。

    【讨论】:

    • 感谢 Fantus 的建议,但我已经看过这个并决定反对它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2010-11-08
    • 2018-09-18
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多