WooCommerce: Automatically Apply Coupon Via URL

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!

URL parameter “code” is present, and its value is the coupon code. With the snippet below, this automatically applies the coupon code to cart without doing it manually!

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

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *