
Automatically Apply WooCommerce Coupons via URL – A Better Checkout Experience
In the default WooCommerce templates, the cart and checkout pages include a coupon input form. While offering discounts is a vital strategy to encourage purchases and reward loyal customers, the visible coupon field can sometimes distract potential buyers. Many customers abandon the checkout process in search of coupon codes, which can hurt your conversion rates.
A great compromise is to apply a coupon automatically when a customer visits a specific URL. This approach keeps the checkout flow smooth and focused, reducing friction and boosting conversions.
In this tutorial, we’ll learn how to automatically apply a coupon in WooCommerce via a URL parameter, creating a seamless and efficient shopping experience for both you and your customers.
How It Works
As shown in the example below, the coupon code is passed as a URL parameter named code
:
https://example.com/?code=xyz
When a customer visits this URL, the coupon is automatically added to their cart — no manual entry needed.
Automatically Apply WooCommerce Coupon from URL Parameter
There are two key parts to making this work:
- Determine the URL parameter — In the example code, we use
code
, but you can change this to any parameter name you like. - Create the coupon in WooCommerce — You must first create the coupon before referencing it in the URL. Depending on your marketing strategy, you can use a meaningful name or a random string.
⚠️ If you want to prevent users from combining multiple coupons by modifying the URL, make sure to enable the “Individual use only” option when creating the coupon. This ensures that only one coupon can be applied per order.
The Code
This implementation contains two functions. Both achieve the same result — automatically applying the coupon — but trigger at different times:
- One function activates when the cart is not empty (
wprs_add_coupon_to_session
). - The other triggers when a product is added to the cart (
wprs_add_coupon_to_cart
).
Add the following code to your theme’s functions.php
file or a custom plugin:
function wprs_add_coupon_to_session()
{
if (empty($_GET['code'])) return;
if (!WC()->session || (WC()->session && !WC()->session->has_session())) {
WC()->session->set_customer_session_cookie(true);
}
$coupon_code = esc_attr($_GET['code']);
WC()->session->set('coupon_code', $coupon_code);
if (WC()->cart && !WC()->cart->has_discount($coupon_code)) {
WC()->cart->calculate_totals();
WC()->cart->add_discount($coupon_code);
WC()->session->__unset('coupon_code');
}
}
add_action('woocommerce_add_to_cart', 'wprs_add_coupon_to_cart');
function wprs_add_coupon_to_cart()
{
$coupon_code = WC()->session ? WC()->session->get('coupon_code') : false;
if (!$coupon_code || empty($coupon_code)) return;
if (WC()->cart && !WC()->cart->has_discount($coupon_code)) {
WC()->cart->calculate_totals();
WC()->cart->add_discount($coupon_code);
WC()->session->__unset('coupon_code');
}
}
Final Thoughts
With just a few lines of code, you can streamline the checkout process and offer automatic discounts using WooCommerce. This method is especially useful for email campaigns, social promotions, or loyalty rewards — all while keeping the user experience smooth.