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' );
}
}