Add coupon code form to Magento Onepage Checkout Review Step

Sometimes you want customers to be able to apply the discount at the end of checkout. For example, when you have a mini cart that links customers straight to “checkout” without viewing the “cart” page.

1. Create a new module (with namespace Ronnyew_Checkout)

File: app/etc/modules/Ronnyew_Checkout.xml

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

2. Override the core Onepagecontroller

File: app/local/Ronneyw/Checkout/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Ronnyew_Checkout>
            <version>0.2.0</version>
        </Ronnyew_Checkout>
    </modules>  

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Ronnyew_Checkout before="Mage_Checkout">Ronnyew_Checkout</Ronnyew_Checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

3. Create a new method that will process the coupon code

File: app/local/Ronneyw/Checkout/controllers/OnepageController.php

<?php

require_once 'Mage/Checkout/controllers/OnepageController.php';

class Ronnyew_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
    
	public function couponAction() {
		$this->loadLayout('checkout_onepage_review');
		$this->couponCode = (string) $this->getRequest()->getParam('coupon_code');
		Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
		Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode(strlen($this->couponCode) ? $this->couponCode : ' ')->collectTotals()->save();
		$result['goto_section'] = 'review';
		$result['update_section'] = array( 'name' => 'review', 'html' => $this->_getReviewHtml() );
		$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
	}

}

4. Add this form to the “review” template

File: app/design/frontend/YOURPACKAGE/YOURTHEME/template/checkout/onepage/review/info.phtml

<?php $getCouponCode = Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode(); ?>
<form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/onepage/coupon')?>" method="post">
<label for="coupon_code"> <?php echo $this->__('Enter your coupon code if you have one.') ?></label><br />
<input id="coupon_code" name="coupon_code" value="<?php echo $getCouponCode; ?>"/>
<?php  if(strlen($getCouponCode)) { ?>
<button type="button" onclick="updateCoupon(2); return false;" value="<?php echo $this->__('Remove Coupon')?>"><span><?php echo $this->__('Remove Coupon') ?></span></button>
<?php } ?>
<button type="button" onclick="updateCoupon(1); return false;" value="<?php echo $this->__('Apply Coupon')?>"><span><?php echo $this->__('Apply Coupon') ?></span></button>
</form>

5. Add this javascript to the onepage.phtml template

File: app/design/frontend/YOURPACKAGE/YOURTHEME/template/checkout/onepage.phtml

<script type="text/javascript">
//<![CDATA[
function updateCoupon(reqid) {
	var reqid;
	if(reqid == 2) {  $('coupon_code').setValue('');  }
	$('discount-coupon-form').request({
		method: 'post',
		onComplete: payment.onComplete,
		onSuccess: payment.onSave,
		onFailure: checkout.ajaxFailure.bind(checkout),
	})
}
//]]>
</script>

6. Style it and add HTML markup to suit your theme