Automatically add coupon in add to cart

You want to offer a product with a coupon discount but would like this done in one step. What you do not want is the customer having to click on a link to add the product to the cart and then have to enter the coupon code. This could be used in an email or some other promotion.

We can go about adding products to cart with coupons. One of the simplest and most effective ways is to use Magento’s observer and event handler. In this case we observer the checkout_cart_add_product_complete event and check for a coupon code. The end result is a url ending with /checkout/cart/add/product/27?coupon_code=abc123

This will take the coupon code and validate/apply it to the product in the cart before displaying the Magento cart page.

We will use the MCorner namespace for this example.
To start create the file app/etc/modules/MCorner_Checkout.xml to enable the module.

<?xml version="1.0"?>

Next create the following directory path app/code/community/MCorner/Checkout/etc.
Create the file config.xml with the following in it. This listens for the event and triggers the apply coupon functionality.

<?xml version="1.0"?>

Now create the directory app/code/community/MCorner/Checkout/Model.
Create the file Observer.php with the following in it. This is the Observer that actually tries to apply the coupon to the cart.


class MCorner_Checkout_Model_Observer extends Varien_Object

	public function applyCoupon(Varien_Event_Observer $observer) {
		$response = $observer->getResponse();
		$request  = $observer->getRequest();
		$couponCode = (string)$request->getParam('coupon_code');
		if (!$couponCode or !strlen($couponCode)) {
		$session    = Mage::getSingleton('checkout/session');
		$cart       = Mage::getSingleton('checkout/cart')->getQuote();
		$cart->setCouponCode(strlen($couponCode) ? $couponCode : '')

Clear your cache, confirm you have a coupon setup and that’s it.
Just a note you will need a valid coupon for the product or cart.

19 thoughts on “Automatically add coupon in add to cart

    • Hi,
      You will need to have a valid and active coupon. Also the coupon should be for the specified product or for the whole cart – not product specific. You can test the coupon as per normal cart functionality and if it works there it should work in the url as above.

  1. Thanks for your quick respond, I appreciate.
    how do you call the link, because the coupon is already enable and is not calling/adding the product to cart.

  2. Now when I call the link, is adding the product to the cart.
    I need to try with the coupon code now

      • Confirm you have cleared Magento’s cache and also that the coupon code is working in the cart normally. Also confirm the product id is correct for the coupon. If it is the try it in the url.

  3. Where is the product id goes?

    $request = $observer->getRequest();
    $couponCode = (string)$request->getParam(‘coupon_code’);
    if (!$couponCode or !strlen($couponCode)) {
    $session = Mage::getSingleton(‘checkout/session’);
    $cart = Mage::getSingleton(‘checkout/cart’)->getQuote();
    $cart->setCouponCode(strlen($couponCode) ? $couponCode : ”)

    • When you try the If it in the url it will add to the cart because of the product id, even when the coupon code is not there

  4. When you try it in the url, it will add to the cart because of the product id, even when the coupon code is not there.

  5. I want it to be able to add the product to the cart when the coupon is applied even when you are just buying other products.

  6. I also have 3 other coupons that already in the walking on the site.
    But, like I said I want this one to be able to add the product to the cart when applied.

    • Hi. I think there is some confusion. This does not add a product to the cart upon submitting a coupon.
      This post describes how to successfully add a product to a cart with the coupon applied at the same time. This can be done from the query string as described.

  7. Hello,

    I Can’t seem to get the module working…. when i use the link, it adds the product to the cart but doesn’t apply the coupon. When i add the coupon manually, it works.

    Anybody knows what the problem could be?

    • Hi vegetaaa,
      Which link are you using – the default product?
      Test it with /checkout/cart/add/product/27?coupon_code=abc123 adjusting the product id for one in your store and the coupon code for a valid coupon code.

      • Hi Sarg,

        Thank you for you response. No it didn’t work… I’m not a great programmer so I de-installed the module.

        Another problem it was giving me: after completing an order, the client was redirected to the cart, with the products still in them. Normally you would expect a thank you for you order message. I use magento There must have been some sort of conflict with another module.

      • vegetaaa, are you sure you cleared all cache? If you have another conflicting module then it is hard to diagnose. To test you could try disabling the other module(s) by temporarily removing their xml file in app/etc/modules/ directory.

Comments are closed.