2

How can i show Promo code or coupon code on Checkout page in magento 1.9. And we can have option in admin panel or we do by code? please help me!!!

  • on which step you want to call coupon code form ? – Pawan Jan 1 at 7:00
  • On right side of checkout page or in payment information on chechkout page. – Muhammad Hamza Jan 1 at 7:10
3

You can call coupon code at checkout by following code:

<div class="custom-coupan">
    <?php echo $this->getLayout()->createBlock('checkout/cart_coupon')
->setTemplate('checkout/cart/coupon.phtml')->toHtml(); ?>
</div>

You need to add code at:

mag1Root/app/design/frontend/{package}/{theme}/template/checkout/onepage/progress.phtml

Note: when you apply coupon code it will redirect to cart page, So need a proper overriding of controller and redirect from where from is submitted.

Update I

If we need to show success/error message on checkout page, we need to create a module say Mago_Coupon

Create

app/etc/modules/Mago_Coupon.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mago_Coupon>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout />
            </depends>
        </Mago_Coupon>
    </modules>
</config>

app/code/local/Mago/Coupon/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mago_Coupon>
      <version>0.1.0</version>
    </Mago_Coupon>
  </modules>
  <frontend>
   <routers>
        <coupon>
              <use>standard</use>
              <args>
                 <module>Mago_Coupon</module>
                 <frontName>coupon</frontName>
             </args>         
        </coupon>
        <checkout>
            <args>
                 <modules>
                     <Mago_Coupon before="Mage_Checkout">Mago_Coupon</Mago_Coupon>
                 </modules>
            </args>
        </checkout>
    </routers>
    </frontend>
</config>

app/code/local/Mago/Coupon/controllers/CartController.php

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php';
class Mago_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();
    }
}

Now Mageto will read this couponPostAction instead of core couponPostAction.

  • Thanks its working but when i apply i want to show message that coupon code is apply or not .... like it show on cart page how can i do that? – Muhammad Hamza Jan 1 at 7:52
  • Welcome @MuhammadHamza. Currently it is showing message on cart page. Right ? – Pawan Jan 1 at 7:55
  • yes ... but not showing on checkout page where i add know – Muhammad Hamza Jan 1 at 7:58
  • As in answer I mentioned We need a proper controller override. This form is submitted on couponPostAction at app/code/core/Mage/Checkout/controllers/CartController.php. If you look at code for this function, it is redirecting to cart page. – Pawan Jan 1 at 8:06
  • I will update my answer for redirecting to checkout page. – Pawan Jan 1 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.