Auto Apply Coupons
M
Michael Walker
Could we please "auto apply" discount codes based on a URL parameter please?
It'd be great to automatically apply a 100% off coupon so that when someone loads the page, they can see the discount applied, but they don't need to manually input the code.
This would be simple – you could use the same logic you use for the "query key" Custom Field forms, and the URL could look something like:
Thank you.
Log In
S
Steve Day
definate bump to this - super useful for auto adding coupons to button links in campaigns so the client can see the real discount.
L
Lucas Garvin
Bump
M
Mike Valera
You can use the follow script: https://gist.github.com/mikevalera/bf9fbdd5b2682f07d9c209f78a252309
It will auto-apply any coupon that is passed via the URL like: https://yoursite.com?coupon_code=#####
Where ##### is your coupon
L
Lucas Garvin
Mike Valera Thanks for the contribution! Really great. Just FYI, this only works with 1-step checkout.
S
Steve Day
Mike Valera thanks for sharing. I tested this and it seems the customer still has to click the 'Apply Coupn' button for it to work. Just checking I've not done something wrong. thanks again
A
Avery Smith
Mike Valera you are the BOMB for sharing this. User still has to press "Apply" but better than nothing
A
Avery Smith
I actually gave the code to ChatGPT & told it the problem and it solved it
Increased Timeout: I increased the setTimeout delay from 500ms to 1000ms. This gives the page a bit more time to fully load the elements before the script tries to click the button. Sometimes elements take a bit longer to render, especially on slower connections or with complex pages.
Added focus(): I added applyCouponButton.focus(); before clicking the button. This ensures the button is in focus before the click event, which can sometimes help with triggering actions that rely on the element being actively focused.
<script>
document.addEventListener("DOMContentLoaded", function () {
function getQueryParam(param) {
var urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}
var couponCode = getQueryParam("coupon_code");
if (couponCode) {
var couponInput = document.querySelector('input[name="coupon_code"]');
if (couponInput) {
couponInput.value = couponCode;
var inputEvent = new Event('input', { bubbles: true });
couponInput.dispatchEvent(inputEvent);
setTimeout(function () {
var applyCouponButton = document.querySelector('.apply-coupon-btn');
if (applyCouponButton) {
applyCouponButton.focus();
}
}, 1000); // Increased timeout
}
}
});
</script>
S
Santi Umaña
Avery Smith where do you add this?
M
Mustafa Salih Oğuz
Avery Smith I couldn't find where to add this as well
M
Michael Walker
another bump... this seems like a relatively easy one to implement!
B
Ben Omlid
This is a must for e-commerce
P
Private User
Absolutely essential for so many of us.
C
Candace Ginn
yasss please!
M
Modern Musician
This would be super helpful, please consider adding it!
M
Michael Walker
bump on this!
G
Geoffrey Nguyen
yes please !
Load More
→