Magento – Discount Coupon in quick checkout review step


If you configure onepage checkout in Magento store, buyers will have no chance to use discount coupon. However, there’s way to do so with little customization. I have implemented coupon in review step of onepage checkout.

1. Override Onepage checkout model – core/Mage/Checkout/controllers/OnepageController.php. Here mainly you have to override _getReviewHTML() function and add function to save/remove coupon code.

function _getReviewHtml()
{
    $text = $this->getLayout()->getBlock('root')->toHtml();

    $text .= '<form id="discount-coupon-form" action="/checkout/onepage/coupon/" method="post">';
    $text .=
'<label for="coupon_code"> '
. $this->__('Enter your coupon code if you have one.') .
'</label><br />';
    //$text .= '<input type="hidden" name="remove" id="remove-coupone" value="0" />';
    //if(!strlen($this->getCouponCode()))
    $text .= '<input class="input-text"
 id="coupon_code" name="coupon_code"
value="' . $this->_getQuote()->getCouponCode() . '"/>';
    $text .= '<input type="hidden" id="coupon" name="coupon" value="1"/>';
    $text .= '   <button type="button" class="button" onclick="updateCupon()"
 value="' . $this->__('Apply Coupon') .'">
<span>' . $this->__('Apply Coupon') .'</span></button>';
    $text .= '</form>';
     
    return $text;
}

/**
 * Coupon check
 */
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));
}
function couponAction() update cart validating coupon number.
2. Edit onepage.phtml and you might to add some javascript code.

<script type="text/javascript">
//<![CDATA[
function updateCupon() {
    $('discount-coupon-form').request({
        method: 'post',
        onComplete: payment.onComplete,
        onSuccess: payment.onSave,
        onFailure: checkout.ajaxFailure.bind(checkout),
    })
}
//]]>
</script>
This script will submit coupon with ajax using post method. And it will update review HTML using regular checkout function.

Comments