Recently, I decided to hide the “Apply coupon” form on the cart and checkout page. A coupon form can “distract” potential customers and have them to abandon the checkout to go looking for discount codes.
However, offering discounts is still a crucial strategy to encourage purchases and reward loyal customers. For example, the Business Bloomer Club lifetime membership is subject to Purchasing Power Parity discounts, and customers can get from 5% to 75% off based on their billing country – via a coupon.
The best compromise here is to apply coupons when customers visit specific URLs. In this way, you can simplify the checkout process, reduce friction, and potentially increase conversion rates.
In this blog post, Iβll guide you through the steps to set up automatic coupon application in WooCommerce, ensuring a seamless and efficient experience for both you and your customers. Enjoy!

PHP Snippet: Programmatically Apply a WooCommerce Coupon Based on URL Parameter
There are two important notes.
First of all, you have to decide the URL parameter you want to use. In the snippet below you will see I use “code”, but of course you can change it to whatever you like. The URL will need to contain the parameter in order to apply the coupon: example.com/?code=xyz
Second, of course you need to create a coupon before adding its code to the URL! You need to consider whether you want a random code, or something prettier, that’s up to you and your marketing strategy. Once you create the coupon, make sure to check the “Individual use only” checkbox in the coupon usage restriction settings (“Check this box if the coupon cannot be used in conjunction with other coupons“) if you don’t want people to add multiple coupons to cart with different URLs.
Other than that, the code below is split into two parts, and they both have the same outcome. The only difference is that one function will work on load if the cart is not empty (bbloomer_add_coupon_to_session), while the other will trigger when you add to cart (bbloomer_add_coupon_to_cart).
/**
* @snippet Apply WooCommerce Coupon On Link Click
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 9
* @community https://businessbloomer.com/club/
*/
add_action( 'wp_loaded', 'bbloomer_add_coupon_to_session' );
function bbloomer_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', 'bbloomer_add_coupon_to_cart' );
function bbloomer_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' );
}
}









We’ve been using this code or a version of it with great success until recently. However, for reasons I do not quite understand at this point, it either stopped working or never did work on coupon types based either on included products or on included product categories. I consider the latter, that it never actually worked, unlikely, since we extensively tested the functions before adding them to the site, but I do not have specific notes or a positive recollection of such testing, so I can’t swear to it.
In recent tests (prompted for other reasons, not specific user reports, for example), the action would simply stall, throwing a “Sorry, coupon [] is not applicable to selected products” error from class-wc-discounts.php. The error message would show, and URL would show as an add-to-cart link with the code enumerated – like [site_url]/cart/?add-to-cart=1198&quantity=1&code=test60. However, contrary to the message, the code COULD be applied, either by addition using normal methods or simply by refreshing the page, at which point, after application, the URL would show as a simple cart link, i.e. [site_url]/cart/
At the same time a simpler coupon with the minimal restrictions (one product excluded) continued to work as expected: Use cart link with coupon code, coupon automatically applied, no issue.
After extensive efforts to debug, I finally discovered that I could prevent the inappropriate error simply by commenting out everything after
in the first function. It appeared redundant anyway, since the lines are identical to ones that run in bbloomer_add_coupon_to_cart(). During my debugging, I also noticed that the session action on wp_loaded runs multiple times per use, while action on woocommerce_add_to_cart runs only once.
So, though I was unable to pin down the error or dysfunction precisely, it appears that it may have getting thrown on some iteration of bbloomer_add_coupon_to_session. Maybe the code enabling inclusion of product ids and categories runs at too late or early a priority relative to wp_loaded. I guess a next experiment might have been trying some other action hook, but simply removing the cart code from the session action seems more parsimonious, and I’m not sure what other function the removed code would servie.
Thanks so much!
Hi. I have never used custom code so I have a question. Do I copy paste this whole thing as is into my theme css or do I need to edit certain parts?
More importantly, I am trying to add a product to the cart with the coupon code embedded.
I currently use the link format example.com/shop/cart?add-to-cart=xxxx where xxxx is my product id. Will this code allow me to both add to cart and add the coupon at the same time? What would the link look like and do I need to make any adjustments to what I paste into the custom css?
Please keep in mind that I really donβt know anything about coding. I currently only use the block editor and plugins.
Hello Brandi! You should add this to your child theme’s functions.php, a custom plugin, or a PHP “Code Snippet” (Code Snippets plugin) – I have a tutorial here: https://www.businessbloomer.com/woocommerce-customization/.
Your URL should look like this once the snippet is enabled:
example.com/shop/cart?add-to-cart=xxxx&code=xyz