Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何防止Magento删除分组项目_Php_Magento_Observers - Fatal编程技术网

Php 如何防止Magento删除分组项目

Php 如何防止Magento删除分组项目,php,magento,observers,Php,Magento,Observers,我已经成功地创建了一个包含4个产品的分组产品,所有产品都工作正常。但是,其中一个项目是免费项目,仅在购买分组产品时可用。我的问题是,当我去篮子,我可以编辑它,并删除一些项目。如果有人从购物篮中编辑一个分组产品并抛出一条消息,有没有办法删除免费物品,这可能吗 我正在使用Magento v1.3.2.4 更新: 我仍然有问题!根据马吕斯的建议,我在app/etc/modules中使用以下代码创建了一个名为FreePins的定制模块/ <?xml version="1.0"?> <c

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

我正在使用Magento v1.3.2.4

更新:

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

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

0.1.0
测试自由销/观察者
删除免费项目
最后,在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;
        }
    }

}

?>


我不完全确定这是否正确,因为一旦添加了物品,我就无法将其从篮子中移除。如果我在模块配置中注释掉前端标记,则站点可以工作,但我的功能不运行,有人可以帮助吗?

您可以为事件创建一个观察者
sales\u quote\u remove\u item
。在此检查中,删除的项目是否为分组产品的一部分。如果是,请同时删除免费产品。
类似这样的内容(将
[module]
替换为您的模块名称): 在模块的
config.xml
中,将其添加到
标记中

<events>
    <sales_quote_remove_item>
       <observers>
           <[module]>
               <class>[module]/observer</class>
                   <method>removeFreeItems</method>
           </[module]
       </observers>
    </sales_quote_remove_item>
</events>
您可以使用“购物车价格规则”来实现这一点。但是,如果您使用这种方法,商品将在购物车中显示完整价格,并将应用折扣。如果你能接受这一点,以下是如何做到这一点:

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

  • 谢谢你的回复,马吕斯。我不完全确定如何制作一个模块,但我发现,一旦我安装了它,我会尝试一下你的代码,然后回帖感谢Fantus的建议,但我已经看过了,并决定不使用它。
    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);
           }
        }
    }