0

I want to remove the SalesRule from Quote if rule Coupon type is

Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON

I have this code:

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

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

    return $this;

which works fine if I want to remove the Coupon code which I set in the salesRule. But same rule does not work for Coupon type which I have mentioned above.

I have tried solutions like iterating through quote Items and setting the appliedRules to null and discount amount, value to zero but nothing worked. E.g.

    $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();
                }
            }

Or also tried setting AppliedRules to null in QUOTE and ADDRESS:

if ($appliedRule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON) {
            $address->setAppliedRuleIds('');
            $quote->setAppliedRuleIds('');
        }

But no success so far. I will be great full if someone can tell me how can I remove the SalesRule from the Quote.

By the way above code is running under

salesrule_validator_process

Event.

1 Answer 1

2

I figured it out myself although I am not sure how efficient this answer is. As previously mentioned the code is executing in the

salesrule_validator_process

this event pass following data with itself:

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

You can find the code in class app/code/core/Mage/SalesRule/Model/Validator.php under function public function process(Mage_Sales_Model_Quote_Item_Abstract $item) of Magento EE

The RESULT contains the information of DISCOUNT AMOUNT and BASE DISCOUNT AMOUNT

I simply changed the value of these two to zero which helps me resolving my problem. Below is the complete code:

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.