Close

February 12, 2014

Magento – Auto Apply Coupon Discount

We had a request to be able to link a coupon from a email newsletter back to magento, and automatically apply a coupon discount with one URL. So for instance, clicking on something like http://yoursite/applycoupon/index/?url=some-cool-product&coupon_code=TESTYOURCOUPON would go the magento site, automatically apply the discount to the cart when the user adds something to their cart.

Natively, this does not exist within Magento, so we had to either build for find a plugin to accomplish the same thing.

We found a great article from Drew Gillson (thank you!) that got us most of the way there. His article should get you most of the way towards the solution, however we still were having problems getting it to work.

Once it was working, we wanted to provide the code (use at your own risk) and explain the differences.

  1. We need to create a new plugin in the app/code/local folder. Most of our plugins are within a “Stech” subfolder.
  2. We named our plugin “Apply Coupon” and created the following files and sub folders:
    Stech/ApplyCoupon/controllers/IndexController.php
    Stech/ApplyCoupon/etc/config.xml
    Stech/ApplyCoupon/Observer/Observer.php
    /app/etc/modules/Stech_ApplyCoupon.xml (needed to turn on plugin)
  3. Code is listed below. The only change we made that differed from Drew’s version is that we do NOT penalize a customer for bringing in an empty coupon (within the IndexController.php file, you’ll see my comments).

After the plugin is setup and no errors are reported on the backend, you should be able to go to the following link and have your coupon silently applied. Once you add an item to the cart, the discount will auto-magically be applied.

http://yoursite/applycoupon/index/?url=some-cool-product&coupon_code=TESTYOURCOUPON

This is intended to help out the community and get some programmers up and running in short order. The code is for demonstration purposes only.

If you need any help of our expert integration services, we are always available! Give us a call or send us a contact message .

Stech_ApplyCoupon.xml

<?xml version="1.0"?>
<config>
	<modules>
		<Stech_ApplyCoupon>
			<active>true</active>
			<codePool>local</codePool>
		</Stech_ApplyCoupon>
	</modules>
</config>

IndexController.php

<?php
//jts: add coupon code to session and wait till they add a product to their cart
//borrowed from: http://www.drewgillson.com/blog/how-to-apply-magento-coupon-codes-automatically/
//testing: http://yoursite/applycoupon/index/?url=some-cool-product&coupon_code=TESTYOURCOUPON
 
class Stech_ApplyCoupon_IndexController extends Mage_Core_Controller_Front_Action {
 
	public function indexAction() {    
    $coupon_code = $this->getRequest()->getParam('coupon_code');
    $code_success=false;
    if ($coupon_code != '') {
        Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
        Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
		Mage::getSingleton('core/session')->addSuccess($this->__('Coupon was automatically applied'));
		    $code_success=true;
    }
    else {
		/*  jts don't punish users for not having coupon code..commented out.
        Mage::getSingleton("checkout/session")->setData("coupon_code","");
		$cart = Mage::getSingleton('checkout/cart');
		foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) {
		    $cart->removeItem( $item->getId() );
		}
		$cart->save();
		*/
    }
 
    if ($this->getRequest()->getParam('url')) {
		//using raw header instead of _redirect because _redirect appends a /
		header('HTTP/1.1 301 Moved Permanently');
		$gclid = $this->getRequest()->getParam('gclid');
		$url = $this->getRequest()->getParam('url');
		//jts add parms for jquery success
		$parms= '?gclid=' . $gclid;
		if($code_success==true):
			$parms.="&code=success";
		endif;
			header('Location: /' . $url .$parms);
			die();
	} else {
        $this->_redirect("/");
    }
}
 
}
?>

Observer.php

<?php
//jts: add coupon code to session and wait till they add a product to their cart
//borrowed from: http://www.drewgillson.com/blog/how-to-apply-magento-coupon-codes-automatically/
//testing: http://yoursite/applycoupon/index/?url=some-cool-product&coupon_code=TESTYOURCOUPON
 
class Stech_ApplyCoupon_IndexController extends Mage_Core_Controller_Front_Action {
 
	public function indexAction() {    
    $coupon_code = $this->getRequest()->getParam('coupon_code');
    $code_success=false;
    if ($coupon_code != '') {
        Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
        Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
		Mage::getSingleton('core/session')->addSuccess($this->__('Coupon was automatically applied'));
		    $code_success=true;
    }
    else {
		/*  jts don't punish users for not having coupon code..commented out.
        Mage::getSingleton("checkout/session")->setData("coupon_code","");
		$cart = Mage::getSingleton('checkout/cart');
		foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) {
		    $cart->removeItem( $item->getId() );
		}
		$cart->save();
		*/
    }
 
    if ($this->getRequest()->getParam('url')) {
		//using raw header instead of _redirect because _redirect appends a /
		header('HTTP/1.1 301 Moved Permanently');
		$gclid = $this->getRequest()->getParam('gclid');
		$url = $this->getRequest()->getParam('url');
		//jts add parms for jquery success
		$parms= '?gclid=' . $gclid;
		if($code_success==true):
			$parms.="&code=success";
		endif;
			header('Location: /' . $url .$parms);
			die();
	} else {
        $this->_redirect("/");
    }
}
 
}
?>

config.xml

<?php
//jts: add coupon code to session and wait till they add a product to their cart
//borrowed from: http://www.drewgillson.com/blog/how-to-apply-magento-coupon-codes-automatically/
//testing: http://yoursite/applycoupon/index/?url=some-cool-product&coupon_code=TESTYOURCOUPON
 
class Stech_ApplyCoupon_IndexController extends Mage_Core_Controller_Front_Action {
 
	public function indexAction() {    
    $coupon_code = $this->getRequest()->getParam('coupon_code');
    $code_success=false;
    if ($coupon_code != '') {
        Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
        Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
		Mage::getSingleton('core/session')->addSuccess($this->__('Coupon was automatically applied'));
		    $code_success=true;
    }
    else {
		/*  jts don't punish users for not having coupon code..commented out.
        Mage::getSingleton("checkout/session")->setData("coupon_code","");
		$cart = Mage::getSingleton('checkout/cart');
		foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) {
		    $cart->removeItem( $item->getId() );
		}
		$cart->save();
		*/
    }
 
    if ($this->getRequest()->getParam('url')) {
		//using raw header instead of _redirect because _redirect appends a /
		header('HTTP/1.1 301 Moved Permanently');
		$gclid = $this->getRequest()->getParam('gclid');
		$url = $this->getRequest()->getParam('url');
		//jts add parms for jquery success
		$parms= '?gclid=' . $gclid;
		if($code_success==true):
			$parms.="&code=success";
		endif;
			header('Location: /' . $url .$parms);
			die();
	} else {
        $this->_redirect("/");
    }
}
 
}
?>