Many times we need to show coupon code
form on checkout page in progress section.This can be achieved by Simple module. first we need to add form on checkout page in following file:
app/design/frontend/{package}/{theme}/template/checkout/onepage/progress.phtml
1 2 3 4 |
<div class="custom-coupon"> <?php echo $this->getLayout()->createBlock('checkout/cart_coupon') ->setTemplate('checkout/cart/coupon.phtml')->toHtml(); ?> </div> |
Above code will Show Coupon form but for Showing success/error message, We need a proper override of couponPostAction
action of CartController
. We are going to create a small module.
create: app/etc/modules/Pawan_Coupon.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0"?> <config> <modules> <Pawan_Coupon> <active>true</active> <codePool>local</codePool> <depends> <Mage_Checkout /> </depends> </Pawan_Coupon> </modules> </config> |
app/code/local/Pawan/Coupon/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0"?> <config> <modules> <Pawan_Coupon> <version>0.1.0</version> </Pawan_Coupon> </modules> <frontend> <routers> <coupon> <use>standard</use> <args> <module>Pawan_Coupon</module> <frontName>coupon</frontName> </args> </coupon> <checkout> <args> <modules> <Pawan_Coupon before="Mage_Checkout">Pawan_Coupon</Pawan_Coupon> </modules> </args> </checkout> </routers> </frontend> </config> |
app/code/local/Pawan/Coupon/controllers/CartController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php'; class Pawan_Coupon_CartController extends Mage_Checkout_CartController { public function couponPostAction() { /** * No reason continue with empty shopping cart */ if (!$this->_getCart()->getQuote()->getItemsCount()) { $this->_redirectReferer(); return; } $couponCode = (string) $this->getRequest()->getParam('coupon_code'); if ($this->getRequest()->getParam('remove') == 1) { $couponCode = ''; } $oldCouponCode = $this->_getQuote()->getCouponCode(); if (!strlen($couponCode) && !strlen($oldCouponCode)) { $this->_redirectReferer(); return; } try { $codeLength = strlen($couponCode); $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH; $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true); $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '') ->collectTotals() ->save(); if ($codeLength) { if ($isCodeLengthValid && $couponCode == $this->_getQuote()->getCouponCode()) { Mage::getSingleton('core/session')->addSuccess( $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode)) ); $this->_getSession()->setCartCouponCode($couponCode); } else { Mage::getSingleton('core/session')->addError($this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode)) ); } } else { Mage::getSingleton('core/session')->addSuccess($this->__('Coupon code was canceled.')); } } catch (Mage_Core_Exception $e) { $this->_getSession()->addError($e->getMessage()); } catch (Exception $e) { $this->_getSession()->addError($this->__('Cannot apply the coupon code.')); Mage::logException($e); } $this->_redirectReferer(); } } |
Final result will be: