I am attempting to apply a coupon code to the basket on a button press. I have the following code:-
coupon = jQuery(this).data('coupon');
data = {coupon_code : coupon};
jQuery.post( "?wc-ajax=apply-coupon", { coupon_code: coupon }).done(function( data ) {
alert( "Data Loaded: " + data );
});
I can see the following data is being parsed to the server:-
coupon_code: 10percentdiscount
The 10percentdiscount
exists.
The Server is not sending back a response once I have sent the request.
Am I correctly making this request or is there another way?
Thank you in advance.
I would do so:
Define ajax url in header.php or in page would you add coupon
In function.php you must define ajax call
In ajax_return.php you past code that add coupon to woocommerce:
Your jQuery.post will became this:
If you need, call return_function to manage response.
You don't need to add custom code in your functions.php. Woocommerce supports adding coupons via AJAX natively (see how below).
Errors in your code:
apply_coupon
notapply-coupon
(underscore vs dash);Correct version: