Add Discount Coupon in Magento One Page Checkout at review step

Step 1: Add following method on checkout controller
app/code/core/Mage/Checkout/controllers/OnepageController.php

function couponAction() {

$this->loadLayout('checkout_onepage_review');
	
	$this->couponCode = (string) $this->getRequest()->getParam('coupon_code');
	$codeLength = strlen($this->couponCode );

	Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
	
	Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode(strlen($this->couponCode) ?
	
	$this->couponCode : ' ')->collectTotals()->save();
	
	if ($codeLength) {
        if ( $this->couponCode == Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode()) {
	        $result['success']='true';
                $result['error'] = false;
	        $result['message']=$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($this->couponCode));
	        } else {
	        $result['success']='false';
	       // $result['error']=true;
	        $result['error_message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($this->couponCode));
	
	        }
	    } else {
	
	    $result['success']='true';
	    $result['error']=false;
	    $result['message']=$this->__('Coupon code was canceled.');
	
	    }
	
	$result['goto_section'] = 'review';
	
	$result['update_section'] = array( 'name' => 'review', 'html' => $this->_getReviewHtml() );
	
	$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

}

Step 2: Add Js script to phtml file
Open file: app/design/frontend/base/default/template/checkout/onepage.phtml
//<![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),

})

}

//]]>

Step 3: Adding form to phtml file

app/design/frontend/base/default/template/checkout/onepage/agreements.phtml

<?php

$CouponCode = Mage::getSingleton(‘checkout/cart’)->getQuote()->getCouponCode(); ?>
<form id=”discount-coupon-form” action=”<?php echo Mage::getBaseUrl()?>/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 $CouponCode; ?>”/>

<?php if(strlen($CouponCode)) { ?>

<button type=”button” onclick=”updateCoupon(2);” value=”<?php echo $this->__(‘Remove Coupon’)?>”><span><?php echo $this->__(‘Remove Coupon’) ?></span></button>

<?php } ?>

<button type=”button” onclick=”updateCoupon(1);” value=”<?php echo $this->__(‘Apply Coupon’)?>”><span><?php echo $this->__(‘Apply Coupon’) ?></span></button>

</form>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s