如果规则优惠券类型为
,我想从报价中删除salesrulemage_salesrule_model_rule :: poufon_type_no_coupon
我有此代码:
$event = $observer->getEvent();
$quote = $event->getQuote();
$address = $quote->getShippingAddress();
$appliedRule = $event->getRule();
if (strlen($quote->getCouponCode()) > 0) {
$quote->setCouponCode('');
$quote->collectTotals()->save();
}
return $this;
如果我想删除我在Salesrule中设置的优惠券代码,则可以正常工作。但是同一规则对我上面提到的优惠券类型不起作用。
我尝试了通过报价项目进行迭代的解决方案,并将 appliedrules 设置为null和折扣金额,值零,但无效。例如
$event = $observer->getEvent();
$quote = $event->getQuote();
$address = $quote->getShippingAddress();
$appliedRule = $event->getRule();
$giftcards = $quote->getGiftCards();
$giftCardsData = unserialize($giftcards);
if ($appliedRule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON) {
foreach($quote->getAllVisibleItems() as $item) {
$item->setAppliedRuleIds('');
$item->setDiscountPercent(0);
$item->setDiscountAmount(0);
$item->setBaseDiscountAmount(0);
$item->save();
}
}
或也尝试在引用和地址中设置为null的设置:
if ($appliedRule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON) {
$address->setAppliedRuleIds('');
$quote->setAppliedRuleIds('');
}
,但到目前为止还没有成功。如果有人能告诉我如何从报价中删除Salesrule,我会很满意。
顺便说一句,上面的代码在
下运行salesrule_validator_process
事件。
我自己弄清楚了,尽管我不确定这个答案的效率有多高。如前所述,代码在
中执行salesrule_validator_process
此事件通过以下数据传递:
Mage::dispatchEvent('salesrule_validator_process', array(
'rule' => $rule,
'item' => $item,
'address' => $address,
'quote' => $quote,
'qty' => $qty,
'result' => $result,
));
您可以在类 App/code/code/core/mage/salesrule/model/model.php 中找到代码公共功能过程(Mage_sales_model_quote_item_abstract $ item(Magento Ee
结果包含折扣金额和基本折扣金额
的信息我只是将这两者的价值更改为零,这有助于我解决问题。以下是完整的代码:
public function YourFunctionName($observer)
{
$event = $observer->getEvent();
$quote = $event->getQuote();
$appliedRule = $event->getRule();
$result = $event->getResult();
$giftcards = $quote->getGiftCards();
$giftCardsData = unserialize($giftcards);
if (!empty($giftCardsData)) {
if (!$appliedRule->getIsUsableWithGiftCard()) {
if ($appliedRule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON) {
$result->setDiscountAmount(0); /// Here it is
$result->setBaseDiscountAmount(0); /// Here it is
} else {
$quote->setCouponCode('');
$quote->collectTotals()->save();
}
}
}
return $this;
}