从Quote Magento 1.x中删除SalesRule

时间:2017-05-02 14:17:14

标签: magento event-handling magento-1.9

如果规则优惠券类型为

,我想从Quote中删除SalesRule
  

Mage_SalesRule_Model_Rule :: COUPON_TYPE_NO_COUPON

我有这段代码:

Filter filter1 = new FilterPredicate("someProperty", FilterOperator.Equal, someValue)
Filter filtrage4 = new StContainsFilter("location", new GeoRegion.Circle(center, radius));
Filter filtrage2 = new FilterPredicate("datenaissance", Query.FilterOperator.LESS_THAN_OR_EQUAL, datemin);

CompositeFilter filter = CompositeFilterOperator.or(filter1, filtrage4, filtrage2);

如果我想删除我在salesRule中设置的优惠券代码,它可以正常工作。但是同样的规则对我上面提到的优惠券类型不起作用。

我尝试过迭代引用项目并将 appliedRules 设置为null,将折扣金额,值设置为零但没有任何效果的解决方案。 E.g。

    $event = $observer->getEvent();
    $quote = $event->getQuote();
    $address = $quote->getShippingAddress();
    $appliedRule = $event->getRule();

    if (strlen($quote->getCouponCode()) > 0) {
        $quote->setCouponCode('');
        $quote->collectTotals()->save();
    }

    return $this;

或者还尝试在QUOTE和ADDRESS中将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();
                }
            }

但到目前为止没有成功。如果有人能告诉我如何从报价中删除SalesRule,我会很满意。

顺便说一下上面的代码是在

下运行的
  

salesrule_validator_process

事件。

1 个答案:

答案 0 :(得分:0)

我自己想通了,虽然我不确定这个答案的效率如何。 如前所述,代码正在

中执行
  

salesrule_validator_process

此事件通过以下数据传递:

Mage::dispatchEvent('salesrule_validator_process', array(
                'rule'    => $rule,
                'item'    => $item,
                'address' => $address,
                'quote'   => $quote,
                'qty'     => $qty,
                'result'  => $result,
            ));

你可以在函数类 app / code / core / Mage / SalesRule / Model / Validator.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;
}