3

I want to add a product to cart automatically when a coupon is applied. When the coupon is applied the product should be added to cart showing its price and a discount of the price of the product needs to be applied.

What I tried is created a coupon code which offers a fixed amount discount which is the price of the product that I want to add to cart. And then I observed the event salesrule_validator_process to check the coupon code applied and tried to add the product to cart. But when the coupon is applied in the cart the above event is fired thrice, so 3 quantities of the product is added to cart. Also every time the cart page is refreshed 2 quantities of the product is again added to cart.

EDIT : How do I remove the product from cart when the coupon is cancelled.

Is there any event which gets fired only once when the coupon is applied or is there any other better method to attain what I want?

2 Answers 2

2
class NameSpace_Module_Model_Observer
{
    $productId = 1;
    $couponId = 'xyz';

    public function yourObserverMethod($observer)
    {
     $quote = $event->getQuote();
     $item = $event->getItem();
        // add
        if ($quote->getCouponCode() == $this->couponId && !in_array($this->productId, $item->getAllIds())){
            // add product
        }

         //remove
        if($quote->getCouponCode() != $this->couponId && in_array($this->productId, $item->getAllIds()){
             // remove product
        }
    }
}

See

Mage::dispatchEvent('salesrule_validator_process', array(
    'rule'    => $rule,
    'item'    => $item,
    'address' => $address,
    'quote'   => $quote,
    'qty'     => $qty,
    'result'  => $result,
));
1
  • thanks for the method. Once the product is added to cart is there a way to prevent the customer from removing the free product from cart?
    – James
    Commented Nov 17, 2014 at 12:11
1

You could expand your observer method as follows:

class NameSpace_Module_Model_Observer
{
    private $_run = true;

    public function yourObserverMethod($observer)
    {
        if ($this->_run):
            $this->_run = false;
            // your code
        }
    }
}

The first time the observer gets called the method will run and the $_run variable will be set to false. At this point the observer object will remain in memory and when the method is called again the $_run variable will still be false and so the remainder of the code will not run a second time

2
  • I recommend to use better explainable variables :-) But the method is the one you want to use. The problem is, the next time the observer runs it will add one again, so, checking for the item in the cart might be better than a local variable which only exists for the request :-) Commented Nov 14, 2014 at 12:22
  • @Jonathan Hussey Thank you. I forgot one thing in my question. How can I remove the product when the coupon is cancelled?
    – James
    Commented Nov 14, 2014 at 12:33

Your Answer

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

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