Php 如何防止Magento删除分组项目
我已经成功地创建了一个包含4个产品的分组产品,所有产品都工作正常。但是,其中一个项目是免费项目,仅在购买分组产品时可用。我的问题是,当我去篮子,我可以编辑它,并删除一些项目。如果有人从购物篮中编辑一个分组产品并抛出一条消息,有没有办法删除免费物品,这可能吗 我正在使用Magento v1.3.2.4 更新: 我仍然有问题!根据马吕斯的建议,我在app/etc/modules中使用以下代码创建了一个名为FreePins的定制模块/Php 如何防止Magento删除分组项目,php,magento,observers,Php,Magento,Observers,我已经成功地创建了一个包含4个产品的分组产品,所有产品都工作正常。但是,其中一个项目是免费项目,仅在购买分组产品时可用。我的问题是,当我去篮子,我可以编辑它,并删除一些项目。如果有人从购物篮中编辑一个分组产品并抛出一条消息,有没有办法删除免费物品,这可能吗 我正在使用Magento v1.3.2.4 更新: 我仍然有问题!根据马吕斯的建议,我在app/etc/modules中使用以下代码创建了一个名为FreePins的定制模块/ <?xml version="1.0"?> <c
<?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>
您可以使用“购物车价格规则”来实现这一点。但是,如果您使用这种方法,商品将在购物车中显示完整价格,并将应用折扣。如果你能接受这一点,以下是如何做到这一点:
谢谢你的回复,马吕斯。我不完全确定如何制作一个模块,但我发现,一旦我安装了它,我会尝试一下你的代码,然后回帖感谢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);
}
}
}