Background:
I have a popup that request emails from users. Should they complete the email submit, I would like to apply a coupon code automatically should they subsequently make a purchase at checkout.
For coupon codes to be applied, all users have to do is access a link with /?coupon_code=xxxx
at the end of the url.
This code was obtained here: GET a coupon code via URL and apply it in WooCommerce Checkout page
Problem:
How do I add the /?coupon_code=xxxx
when a user closes the popup?
What I have tried:
I have tried to use the solution offered here (Change URL without refresh the page). This is what I inserted.
history.pushState(null, '', '/?coupon_code=xxxx');
While this works in changing the url, I find that the coupon code is not applied at checkout. However, if I manually entered the full url and refreshed the page eg. www.test.com/?coupon_code=xxxx
, the coupon code will be applied at checkout.
Thank you.