1

I have a step added to checkout where i want to put some pre defined coupon codes buttons so the user can just click a button to apply the code if they want a sample for example.

For now I can apply a coupon with below form however causes a page reload on checkout:

<form id="discount-coupon-form-ajax" action="/checkout/cart/couponPost/" method="post">
    <input id="coupon_code" class="input-text" name="coupon_code" type="hidden" value="FREESAMPLE" /> 
    <button class="action apply primary" type="submit" value="Apply Discount"> 
        <span>Free Sample</span> 
    </button>
</form>

The checkout however has a much more elegant ajax version of applying a coupon code however and was wondering how i could harness this functionality?

I have tried with some standard jquery like below:

$(document).on("submit", '#discount-coupon-form-ajax', function(event) {  
      event.preventDefault(); 
      $.ajax({ 
        url:"/checkout/cart/couponPost", 
        type:'POST', 
        data:'ajax=true&'+$('#discount-coupon-form-ajax').serialize(), 
        success:function(data) 
        { 
            console.log(data);
        } 
      }); 
    }); 

This however was for Magento 1 I believe and does not respond in json and just seems to respond a standard checkout page. Not sure how to get the coupon controller to respond in json or however it normally responds or if this is possible anymore with code like above.

Your Answer

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

Browse other questions tagged or ask your own question.