Magento – Discount Coupon in quick checkout review step

In Magento on June 15th, 2011

For Magento 1.4.1
———–

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.
Magento - discount coupon in 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.

  1. function _getReviewHtml()
  2. {
  3.  $text = $this->getLayout()->getBlock('root')->toHtml();
  4.  
  5.  $text .= '<form id="discount-coupon-form" action="/checkout/onepage/coupon/" method="post">';
  6.  $text .= '<label for="coupon_code"> ' . $this->__('Enter your coupon code if you have one.') . '</label><br />';
  7.  //$text .= '<input type="hidden" name="remove" id="remove-coupone" value="0" />';
  8.  //if(!strlen($this->getCouponCode()))
  9.  $text .= '<input class="input-text" id="coupon_code" name="coupon_code" value="' . $this->_getQuote()->getCouponCode() . '"/>';
  10.  $text .= '<input type="hidden" id="coupon" name="coupon" value="1"/>';
  11.  $text .= '&nbsp;&nbsp;&nbsp;<button type="button" class="button" onclick="updateCupon()" value="' . $this->__('Apply Coupon') .'"><span>' . $this->__('Apply Coupon') .'</span></button>';
  12.  $text .= '</form>';
  13.  
  14.  return $text;
  15. }
  16.  
  17. /**
  18.  * Coupon check
  19.  */
  20. function couponAction()
  21. {
  22.  $this->loadLayout('checkout_onepage_review');
  23.  
  24.  $this->couponCode = (string) $this->getRequest()->getParam('coupon_code');
  25.  
  26.  Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  27.  Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode(strlen($this->couponCode) ? $this->couponCode : '')
  28.    ->collectTotals()
  29.    ->save();
  30.  
  31.  $result['goto_section'] = 'review';
  32.  $result['update_section'] = array(
  33.   'name' => 'review',
  34.   'html' => $this->_getReviewHtml()
  35.  );
  36.    
  37.  $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  38. }

couponAction() update cart validating coupon number. May be you want to improve _getReviewHTML(), but I wanted quick solution, and without modifying core.

2. Edit onepage.phtml template. You need to add some javascript code.

  1. <script type="text/javascript">
  2. //<![CDATA[
  3. function updateCupon() {
  4.  $('discount-coupon-form').request({
  5.   method: 'post',
  6.   onComplete: payment.onComplete,
  7.   onSuccess: payment.onSave,
  8.   onFailure: checkout.ajaxFailure.bind(checkout),
  9.  })
  10. }
  11. //]]>
  12. </script>

This script will submit coupon with ajax using post method. And it will update review HTML using regular checkout function.

For Magento 1.4.1

  • Ron

    Hi Krishna,
    I was actually looking for something like that since magento is programmed in such way that if you are redirected to the checkout rather than to the cart, there is no where to use the coupon…
    Any way, I was trying to implement it on 1.5 and without any success, can you specify what exactly should  doI remove/change/replace and where to do it?
    Thanks!
    Ron

  • demonkoryu

    Thanks, you saved me a bunch of work.

  • Anonymous

    @Ron:disqus this is for Magento 1.4.1. If you read blog carefully, every step is mentioned clearly. I m not sure how it will go with 1.5, sorry. @demonkoryu:disqus  u welcome

  • Ron

    Thanks. I’ll try to figure out how to implement it in 1.5.1

  • Jason

    Did you figure this out?