Skip to main content
added 2 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
replaced http://magento.stackexchange.com/ with https://magento.stackexchange.com/
Source Link
added 2841 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
  1. Making edits to core controllers OnepageController.php

  2. How to override a controller in Magento [How to override a controller in Magento][1]

Step2: Then on the override controller Amit_CustomCheckout_OnepageController add a function applycouponAction,in where you have send the ajax request to apply coupon and return update content of review step.

<?php 
require_once Mage::getModuleDir('controllers', "Mage_Checkout").DS."IndexController.php";
class Amit_CustomCheckout_OnepageController extends Mage_Checkout_OnepageController
{
    public function applycouponAction(){        /**
         * No reason continue with empty shopping cart
         */
        if (!$this->getOnepage()->getQuote()->getItemsCount()) {
                    $result['error']=true;
                    $result['message']=$this->__('No item in cart.');
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                    
            return;
        }

        $couponCode = (string) $this->getRequest()->getParam('coupon_code');
        if ($this->getRequest()->getParam('remove') == 1) {
            $couponCode = '';
        }
        $oldCouponCode = $this->getOnepage()->getQuote()->getCouponCode();

        if (!strlen($couponCode) && !strlen($oldCouponCode)) {
                    $result['error']=true;
                    $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
            return;
        }

        try {
            $codeLength = strlen($couponCode);
            $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;

            $this->getOnepage()->getQuote()->getShippingAddress()->setCollectShippingRates(true);
            $this->getOnepage()->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
                ->collectTotals()
                ->save();

            $result=array();

            if ($codeLength) {
                if ($isCodeLengthValid && $couponCode == $this->getOnepage()->getQuote()->getCouponCode()) {
                    $result['error']=false;
                    $result['message']=$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                    $result['html']=$this->_getReviewHtml();
                } else {
                    $result['error']=true;
                    $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                }
            } else {

                    $result['error']=false;
                    $result['message']=$this->__('Coupon code was canceled.');
                    $result['html']=$this->_getReviewHtml();

            }

        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
                    $result['error']=true;
                    $result['message']=$this->__('Cannot apply the coupon code.');
            
        } catch (Exception $e) {
                    $result['error']=true;
                    $result['message']=$this->__('Cannot apply the coupon code.');
                    Mage::logException($e);
        }
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        
}
}

In this code, i have return json data with a flag field name errooe  .html return contend of handler checkout_onepage_review and check this handler file at checkout.xml

Then basic on this logic you need to update content [1]: http://www.amitbera.com/how-to-override-a-controller-in-magento/

  1. Making edits to core controllers OnepageController.php

  2. How to override a controller in Magento

Step2: Then on the override controller Amit_CustomCheckout_OnepageController add a function applycouponAction,in where you have send the ajax request to apply coupon and return update content of review step.

  1. Making edits to core controllers OnepageController.php

  2. [How to override a controller in Magento][1]

Step2: Then on the override controller Amit_CustomCheckout_OnepageController add a function applycouponAction,in where you have send the ajax request to apply coupon and return update content of review step.

<?php 
require_once Mage::getModuleDir('controllers', "Mage_Checkout").DS."IndexController.php";
class Amit_CustomCheckout_OnepageController extends Mage_Checkout_OnepageController
{
    public function applycouponAction(){        /**
         * No reason continue with empty shopping cart
         */
        if (!$this->getOnepage()->getQuote()->getItemsCount()) {
                    $result['error']=true;
                    $result['message']=$this->__('No item in cart.');
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                    
            return;
        }

        $couponCode = (string) $this->getRequest()->getParam('coupon_code');
        if ($this->getRequest()->getParam('remove') == 1) {
            $couponCode = '';
        }
        $oldCouponCode = $this->getOnepage()->getQuote()->getCouponCode();

        if (!strlen($couponCode) && !strlen($oldCouponCode)) {
                    $result['error']=true;
                    $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
            return;
        }

        try {
            $codeLength = strlen($couponCode);
            $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;

            $this->getOnepage()->getQuote()->getShippingAddress()->setCollectShippingRates(true);
            $this->getOnepage()->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
                ->collectTotals()
                ->save();

            $result=array();

            if ($codeLength) {
                if ($isCodeLengthValid && $couponCode == $this->getOnepage()->getQuote()->getCouponCode()) {
                    $result['error']=false;
                    $result['message']=$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                    $result['html']=$this->_getReviewHtml();
                } else {
                    $result['error']=true;
                    $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                }
            } else {

                    $result['error']=false;
                    $result['message']=$this->__('Coupon code was canceled.');
                    $result['html']=$this->_getReviewHtml();

            }

        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
                    $result['error']=true;
                    $result['message']=$this->__('Cannot apply the coupon code.');
            
        } catch (Exception $e) {
                    $result['error']=true;
                    $result['message']=$this->__('Cannot apply the coupon code.');
                    Mage::logException($e);
        }
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        
}
}

In this code, i have return json data with a flag field name errooe  .html return contend of handler checkout_onepage_review and check this handler file at checkout.xml

Then basic on this logic you need to update content [1]: http://www.amitbera.com/how-to-override-a-controller-in-magento/

Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
Loading