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>
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

  • 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));
    }

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.