Apply Magento Coupon in Ajax Without Redirecting to Cart Page Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar ManaraCustom AJAX discount-form not processing couponsNeed help in add to cart with ajaxrefresh page in magento without reloading - ajaxAjax redirecting to system configuration page in admin custom moduleMagento Multiple coupon apply on cartsame coupon code should apply for diferent conditionsCoupon code form and show div onSuccess / onFailureApply coupon code only for specific categoryMagento 2:- Apply custom coupon codeDiscount/Coupon Code Block not working on onepage checkout pageCreate an AJAX coupon code button on checkout step

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

Expansion//Explosion and Siren Stormtamer

Raising a bilingual kid. When should we introduce the majority language?

std::is_constructible on incomplete types

Married in secret, can marital status in passport be changed at a later date?

Would reducing the reference voltage of an ADC have any effect on accuracy?

What is this word supposed to be?

Could Neutrino technically as side-effect, incentivize centralization of the bitcoin network?

Seek and ye shall find

My admission is revoked after accepting the admission offer

Is accepting an invalid credit card number a security issue?

"Rubric" as meaning "signature" or "personal mark" -- is this accepted usage?

Why does the Cisco show run command not show the full version, while the show version command does?

My bank got bought out, am I now going to have to start filing tax returns in a different state?

Is Bran literally the world's memory?

Function to calculate red-edgeNDVI in Google Earth Engine

Passing args from the bash script to the function in the script

Has a Nobel Peace laureate ever been accused of war crimes?

What do you call the part of a novel that is not dialog?

Second order approximation of the loss function (Deep learning book, 7.33)

What's parked in Mil Moscow helicopter plant?

Where did Arya get these scars?

Do I need to protect SFP ports and optics from dust/contaminants? If so, how?

How to keep bees out of canned beverages?



Apply Magento Coupon in Ajax Without Redirecting to Cart Page



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraCustom AJAX discount-form not processing couponsNeed help in add to cart with ajaxrefresh page in magento without reloading - ajaxAjax redirecting to system configuration page in admin custom moduleMagento Multiple coupon apply on cartsame coupon code should apply for diferent conditionsCoupon code form and show div onSuccess / onFailureApply coupon code only for specific categoryMagento 2:- Apply custom coupon codeDiscount/Coupon Code Block not working on onepage checkout pageCreate an AJAX coupon code button on checkout step



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















I have coupon code on sidebar on my product categories list, cart and checkout page. Whenever I'm trying to add coupon code while being on the product or category list page or checkout page, it is taking me to the cart page. Is there any way I can make it apply on the same page without redirecting or reloading with the help of ajax?



<?php echo $this->getMessagesBlock()->toHtml(); ?>
<div class="coupon bg-gray">
<form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/coupon') ?>" name="discountcode-form" method="post">
<div class="discount">
<h2><?php echo $this->__('Discount Codes') ?></h2>
<div class="discount-form">
<!--<label for="coupon_code"><?php echo $this->__('Discount Codes') ?></label>-->
<input type="hidden" name="remove" id="remove-coupone" value="0" />
<!--<div class="field-wrapper">-->
<input class=" form-control" type="text" placeholder="Enter Coupon Code" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" />
<!--<div class="button-wrapper">-->
<!-- <button type="button" title="<?php echo $this->__('Apply') ?>" class="btn-danger" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply') ?>"><span><span><?php echo $this->__('REEDEM COUPON') ?></span></span></button>--><?php $block = $this->getLayout()->createBlock('checkout/onepage_link'); ?> <?php if ($block->isPossibleOnepageCheckout()):?> <?php if ($block->isDisabled()):?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger btn-checkoutno-checkout" disabled="disabled" /> <?php else: ?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger" />
<?php endif?> <?php endif?>


<?php if(strlen($this->getCouponCode())): ?>
<button type="button" title="<?php echo $this->__('Cancel') ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel') ?>"><span><span><?php echo $this->__('Cancel') ?></span></span>
</button>
<?php endif;?>

<!--</div>-->
<!--</div>-->
</div>
</div>
</form>
</div>
<script type="text/javascript">

//<![CDATA[

function updateCoupon(reqid)

var reqid;

if(reqid == 2) $('coupon_code').setValue('');

$('discount-coupon-form').request(

method: 'post',

onComplete: payment.onComplete,

onSuccess: payment.onSave,

onFailure: checkout.ajaxFailure.bind(checkout),

)



//]]>

</script>









share|improve this question
















bumped to the homepage by Community 6 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.





















    4















    I have coupon code on sidebar on my product categories list, cart and checkout page. Whenever I'm trying to add coupon code while being on the product or category list page or checkout page, it is taking me to the cart page. Is there any way I can make it apply on the same page without redirecting or reloading with the help of ajax?



    <?php echo $this->getMessagesBlock()->toHtml(); ?>
    <div class="coupon bg-gray">
    <form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/coupon') ?>" name="discountcode-form" method="post">
    <div class="discount">
    <h2><?php echo $this->__('Discount Codes') ?></h2>
    <div class="discount-form">
    <!--<label for="coupon_code"><?php echo $this->__('Discount Codes') ?></label>-->
    <input type="hidden" name="remove" id="remove-coupone" value="0" />
    <!--<div class="field-wrapper">-->
    <input class=" form-control" type="text" placeholder="Enter Coupon Code" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" />
    <!--<div class="button-wrapper">-->
    <!-- <button type="button" title="<?php echo $this->__('Apply') ?>" class="btn-danger" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply') ?>"><span><span><?php echo $this->__('REEDEM COUPON') ?></span></span></button>--><?php $block = $this->getLayout()->createBlock('checkout/onepage_link'); ?> <?php if ($block->isPossibleOnepageCheckout()):?> <?php if ($block->isDisabled()):?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger btn-checkoutno-checkout" disabled="disabled" /> <?php else: ?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger" />
    <?php endif?> <?php endif?>


    <?php if(strlen($this->getCouponCode())): ?>
    <button type="button" title="<?php echo $this->__('Cancel') ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel') ?>"><span><span><?php echo $this->__('Cancel') ?></span></span>
    </button>
    <?php endif;?>

    <!--</div>-->
    <!--</div>-->
    </div>
    </div>
    </form>
    </div>
    <script type="text/javascript">

    //<![CDATA[

    function updateCoupon(reqid)

    var reqid;

    if(reqid == 2) $('coupon_code').setValue('');

    $('discount-coupon-form').request(

    method: 'post',

    onComplete: payment.onComplete,

    onSuccess: payment.onSave,

    onFailure: checkout.ajaxFailure.bind(checkout),

    )



    //]]>

    </script>









    share|improve this question
















    bumped to the homepage by Community 6 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      4












      4








      4








      I have coupon code on sidebar on my product categories list, cart and checkout page. Whenever I'm trying to add coupon code while being on the product or category list page or checkout page, it is taking me to the cart page. Is there any way I can make it apply on the same page without redirecting or reloading with the help of ajax?



      <?php echo $this->getMessagesBlock()->toHtml(); ?>
      <div class="coupon bg-gray">
      <form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/coupon') ?>" name="discountcode-form" method="post">
      <div class="discount">
      <h2><?php echo $this->__('Discount Codes') ?></h2>
      <div class="discount-form">
      <!--<label for="coupon_code"><?php echo $this->__('Discount Codes') ?></label>-->
      <input type="hidden" name="remove" id="remove-coupone" value="0" />
      <!--<div class="field-wrapper">-->
      <input class=" form-control" type="text" placeholder="Enter Coupon Code" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" />
      <!--<div class="button-wrapper">-->
      <!-- <button type="button" title="<?php echo $this->__('Apply') ?>" class="btn-danger" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply') ?>"><span><span><?php echo $this->__('REEDEM COUPON') ?></span></span></button>--><?php $block = $this->getLayout()->createBlock('checkout/onepage_link'); ?> <?php if ($block->isPossibleOnepageCheckout()):?> <?php if ($block->isDisabled()):?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger btn-checkoutno-checkout" disabled="disabled" /> <?php else: ?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger" />
      <?php endif?> <?php endif?>


      <?php if(strlen($this->getCouponCode())): ?>
      <button type="button" title="<?php echo $this->__('Cancel') ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel') ?>"><span><span><?php echo $this->__('Cancel') ?></span></span>
      </button>
      <?php endif;?>

      <!--</div>-->
      <!--</div>-->
      </div>
      </div>
      </form>
      </div>
      <script type="text/javascript">

      //<![CDATA[

      function updateCoupon(reqid)

      var reqid;

      if(reqid == 2) $('coupon_code').setValue('');

      $('discount-coupon-form').request(

      method: 'post',

      onComplete: payment.onComplete,

      onSuccess: payment.onSave,

      onFailure: checkout.ajaxFailure.bind(checkout),

      )



      //]]>

      </script>









      share|improve this question
















      I have coupon code on sidebar on my product categories list, cart and checkout page. Whenever I'm trying to add coupon code while being on the product or category list page or checkout page, it is taking me to the cart page. Is there any way I can make it apply on the same page without redirecting or reloading with the help of ajax?



      <?php echo $this->getMessagesBlock()->toHtml(); ?>
      <div class="coupon bg-gray">
      <form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/coupon') ?>" name="discountcode-form" method="post">
      <div class="discount">
      <h2><?php echo $this->__('Discount Codes') ?></h2>
      <div class="discount-form">
      <!--<label for="coupon_code"><?php echo $this->__('Discount Codes') ?></label>-->
      <input type="hidden" name="remove" id="remove-coupone" value="0" />
      <!--<div class="field-wrapper">-->
      <input class=" form-control" type="text" placeholder="Enter Coupon Code" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" />
      <!--<div class="button-wrapper">-->
      <!-- <button type="button" title="<?php echo $this->__('Apply') ?>" class="btn-danger" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply') ?>"><span><span><?php echo $this->__('REEDEM COUPON') ?></span></span></button>--><?php $block = $this->getLayout()->createBlock('checkout/onepage_link'); ?> <?php if ($block->isPossibleOnepageCheckout()):?> <?php if ($block->isDisabled()):?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger btn-checkoutno-checkout" disabled="disabled" /> <?php else: ?> <input type="submit" title="Minimum order should be Rs.300 or above" value="<?php echo $this->__('REEDEM COUPON') ?>" class="btn-danger" />
      <?php endif?> <?php endif?>


      <?php if(strlen($this->getCouponCode())): ?>
      <button type="button" title="<?php echo $this->__('Cancel') ?>" class="button2 cancel-coupon" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel') ?>"><span><span><?php echo $this->__('Cancel') ?></span></span>
      </button>
      <?php endif;?>

      <!--</div>-->
      <!--</div>-->
      </div>
      </div>
      </form>
      </div>
      <script type="text/javascript">

      //<![CDATA[

      function updateCoupon(reqid)

      var reqid;

      if(reqid == 2) $('coupon_code').setValue('');

      $('discount-coupon-form').request(

      method: 'post',

      onComplete: payment.onComplete,

      onSuccess: payment.onSave,

      onFailure: checkout.ajaxFailure.bind(checkout),

      )



      //]]>

      </script>






      ce-1.9.1.0 ajax coupon-codes






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 5 '18 at 4:54









      Teja Bhagavan Kollepara

      2,99241950




      2,99241950










      asked Jun 30 '15 at 11:43









      Shoeb MirzaShoeb Mirza

      181213




      181213





      bumped to the homepage by Community 6 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 6 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















          2 Answers
          2






          active

          oldest

          votes


















          0














          There are lot issue in your code.



          JavaScript is wrong.you have call opcheckout.js function payment.payment.onSave and checkout.ajaxFailure.bind(checkout), etc which does not work on all pages.



          So you need to Custom code which will apply coupon code on ajax on all pages



          You can follow the link for full your requirement:



          Custom AJAX discount-form not processing coupons



          lelandcope






          share|improve this answer

























          • Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

            – Shoeb Mirza
            Jun 30 '15 at 21:02


















          0














          I have created one module for discount code popup which has method to apply code without reloading cart page



          Here is link for this module for reference



          in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action



          public function applyAction()

          $response = array();
          $couponCode = (string) $this->getRequest()->getParam('coupon_code');
          if ($this->getRequest()->getParam('remove') == 1)
          $couponCode = '';

          $oldCouponCode = $this->_getQuote()->getCouponCode();
          if (!strlen($couponCode) && !strlen($oldCouponCode))
          $message = "Please enter valid code";
          $response['error'] = 1;

          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())
          $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
          $this->_getSession()->setCartCouponCode($couponCode);
          else
          $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
          $response['error'] = 1;

          else
          $message = $this->__('Coupon code was canceled.');

          catch (Mage_Core_Exception $e)
          $message = $e->getMessage();
          $response['error'] = 1;
          catch (Exception $e)
          $response['error'] = 1;
          $message = $this->__('Cannot apply the coupon code.');
          Mage::logException($e);

          $response['message'] = $message;
          $response['code'] = $couponCode;
          $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));






          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f72629%2fapply-magento-coupon-in-ajax-without-redirecting-to-cart-page%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            There are lot issue in your code.



            JavaScript is wrong.you have call opcheckout.js function payment.payment.onSave and checkout.ajaxFailure.bind(checkout), etc which does not work on all pages.



            So you need to Custom code which will apply coupon code on ajax on all pages



            You can follow the link for full your requirement:



            Custom AJAX discount-form not processing coupons



            lelandcope






            share|improve this answer

























            • Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

              – Shoeb Mirza
              Jun 30 '15 at 21:02















            0














            There are lot issue in your code.



            JavaScript is wrong.you have call opcheckout.js function payment.payment.onSave and checkout.ajaxFailure.bind(checkout), etc which does not work on all pages.



            So you need to Custom code which will apply coupon code on ajax on all pages



            You can follow the link for full your requirement:



            Custom AJAX discount-form not processing coupons



            lelandcope






            share|improve this answer

























            • Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

              – Shoeb Mirza
              Jun 30 '15 at 21:02













            0












            0








            0







            There are lot issue in your code.



            JavaScript is wrong.you have call opcheckout.js function payment.payment.onSave and checkout.ajaxFailure.bind(checkout), etc which does not work on all pages.



            So you need to Custom code which will apply coupon code on ajax on all pages



            You can follow the link for full your requirement:



            Custom AJAX discount-form not processing coupons



            lelandcope






            share|improve this answer















            There are lot issue in your code.



            JavaScript is wrong.you have call opcheckout.js function payment.payment.onSave and checkout.ajaxFailure.bind(checkout), etc which does not work on all pages.



            So you need to Custom code which will apply coupon code on ajax on all pages



            You can follow the link for full your requirement:



            Custom AJAX discount-form not processing coupons



            lelandcope







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:54









            Community

            1




            1










            answered Jun 30 '15 at 12:14









            Amit BeraAmit Bera

            60.2k1678178




            60.2k1678178












            • Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

              – Shoeb Mirza
              Jun 30 '15 at 21:02

















            • Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

              – Shoeb Mirza
              Jun 30 '15 at 21:02
















            Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

            – Shoeb Mirza
            Jun 30 '15 at 21:02





            Thanks.. I followed the same tutorial but it is not working.. coupon code is also not working nor ajax..It is giving me "error message"

            – Shoeb Mirza
            Jun 30 '15 at 21:02













            0














            I have created one module for discount code popup which has method to apply code without reloading cart page



            Here is link for this module for reference



            in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action



            public function applyAction()

            $response = array();
            $couponCode = (string) $this->getRequest()->getParam('coupon_code');
            if ($this->getRequest()->getParam('remove') == 1)
            $couponCode = '';

            $oldCouponCode = $this->_getQuote()->getCouponCode();
            if (!strlen($couponCode) && !strlen($oldCouponCode))
            $message = "Please enter valid code";
            $response['error'] = 1;

            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())
            $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
            $this->_getSession()->setCartCouponCode($couponCode);
            else
            $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
            $response['error'] = 1;

            else
            $message = $this->__('Coupon code was canceled.');

            catch (Mage_Core_Exception $e)
            $message = $e->getMessage();
            $response['error'] = 1;
            catch (Exception $e)
            $response['error'] = 1;
            $message = $this->__('Cannot apply the coupon code.');
            Mage::logException($e);

            $response['message'] = $message;
            $response['code'] = $couponCode;
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));






            share|improve this answer





























              0














              I have created one module for discount code popup which has method to apply code without reloading cart page



              Here is link for this module for reference



              in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action



              public function applyAction()

              $response = array();
              $couponCode = (string) $this->getRequest()->getParam('coupon_code');
              if ($this->getRequest()->getParam('remove') == 1)
              $couponCode = '';

              $oldCouponCode = $this->_getQuote()->getCouponCode();
              if (!strlen($couponCode) && !strlen($oldCouponCode))
              $message = "Please enter valid code";
              $response['error'] = 1;

              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())
              $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
              $this->_getSession()->setCartCouponCode($couponCode);
              else
              $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
              $response['error'] = 1;

              else
              $message = $this->__('Coupon code was canceled.');

              catch (Mage_Core_Exception $e)
              $message = $e->getMessage();
              $response['error'] = 1;
              catch (Exception $e)
              $response['error'] = 1;
              $message = $this->__('Cannot apply the coupon code.');
              Mage::logException($e);

              $response['message'] = $message;
              $response['code'] = $couponCode;
              $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));






              share|improve this answer



























                0












                0








                0







                I have created one module for discount code popup which has method to apply code without reloading cart page



                Here is link for this module for reference



                in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action



                public function applyAction()

                $response = array();
                $couponCode = (string) $this->getRequest()->getParam('coupon_code');
                if ($this->getRequest()->getParam('remove') == 1)
                $couponCode = '';

                $oldCouponCode = $this->_getQuote()->getCouponCode();
                if (!strlen($couponCode) && !strlen($oldCouponCode))
                $message = "Please enter valid code";
                $response['error'] = 1;

                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())
                $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                $this->_getSession()->setCartCouponCode($couponCode);
                else
                $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                $response['error'] = 1;

                else
                $message = $this->__('Coupon code was canceled.');

                catch (Mage_Core_Exception $e)
                $message = $e->getMessage();
                $response['error'] = 1;
                catch (Exception $e)
                $response['error'] = 1;
                $message = $this->__('Cannot apply the coupon code.');
                Mage::logException($e);

                $response['message'] = $message;
                $response['code'] = $couponCode;
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));






                share|improve this answer















                I have created one module for discount code popup which has method to apply code without reloading cart page



                Here is link for this module for reference



                in your javascript method updateCoupon() you have call ajax method which calls controller action in that you need to add following code to apply code without refreshing page Here applyAction is my ajax action



                public function applyAction()

                $response = array();
                $couponCode = (string) $this->getRequest()->getParam('coupon_code');
                if ($this->getRequest()->getParam('remove') == 1)
                $couponCode = '';

                $oldCouponCode = $this->_getQuote()->getCouponCode();
                if (!strlen($couponCode) && !strlen($oldCouponCode))
                $message = "Please enter valid code";
                $response['error'] = 1;

                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())
                $message = $this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
                $this->_getSession()->setCartCouponCode($couponCode);
                else
                $message = $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
                $response['error'] = 1;

                else
                $message = $this->__('Coupon code was canceled.');

                catch (Mage_Core_Exception $e)
                $message = $e->getMessage();
                $response['error'] = 1;
                catch (Exception $e)
                $response['error'] = 1;
                $message = $this->__('Cannot apply the coupon code.');
                Mage::logException($e);

                $response['message'] = $message;
                $response['code'] = $couponCode;
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 23 '18 at 6:39









                Jaimin

                1,156729




                1,156729










                answered May 11 '17 at 11:05









                Vaibhav AhalparaVaibhav Ahalpara

                3,67542762




                3,67542762



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f72629%2fapply-magento-coupon-in-ajax-without-redirecting-to-cart-page%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

                    Fil:Tokke komm.svg

                    Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?