Magento – Apply Magento Coupon in Ajax Without Redirecting to Cart Page

ajaxce-1.9.1.0coupon-codes

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>

Best 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