Creating unique WooCommerce URLs that automatically applies a discount to the customer’s shopping cart is a great way to through UX to increase the revenue. By adding a coupon code via url makes it easier for the customer to complete the order in less hustle and time.

Lets see how to apply a coupon via url in WooCommerce without a plugin. First, you will need to set up a coupon in WooCommerce. Then in your child theme’s functions.php add the following code:

PHP Snippet: Apply coupon to WooCommerce cart via url

function webroom_woocommerce_coupon_links(){

	// Bail if WooCommerce or sessions aren't available.

	if (!function_exists('WC') || !WC()->session) {
		return;
	}

	/**
	 * Filter the coupon code query variable name.
	 *
	 * @since 1.0.0
	 *
	 * @param string $query_var Query variable name.
	 */
	$query_var = apply_filters('woocommerce_coupon_links_query_var', 'coupon_code');

	// Bail if a coupon code isn't in the query string.

	if (empty($_GET[$query_var])) {
		return;
	}

	// Set a session cookie to persist the coupon in case the cart is empty.

	WC()->session->set_customer_session_cookie(true);

	// Apply the coupon to the cart if necessary.

	if (!WC()->cart->has_discount($_GET[$query_var])) {

		// WC_Cart::add_discount() sanitizes the coupon code.

		WC()->cart->add_discount($_GET[$query_var]);
	}
}
add_action('wp_loaded', 'webroom_woocommerce_coupon_links', 30);
add_action('woocommerce_add_to_cart', 'webroom_woocommerce_coupon_links');

So what does this code do? You are now able to create a link, containing the coupon code and that coupon will apply to the customer’s cart whether it has products in it or not. The url will contain a coupon_code query argument.

https://examplestore.com/cart/?coupon_code=5dollarOff

The coupon code can be applied in any page in your website, for example:

https://examplestore.com/?coupon_code=5dollarOff
https://examplestore.com/checkout/?coupon_code=5dollarOff
https://examplestore.com/blog/?coupon_code=5dollarOff 

The above will still work and the coupon will be applied to the customers cart.

Adding product and coupon in single url in WooCommerce

You can add single or multiple products to WooCommerce cart via url and it looks like this:

https://www.example.com/checkout/?add-to-cart=12345

Lets combine adding a product and coupon code at the same time via single url in WooCommerce:

https://examplestore.com/checkout/?add-to-cart=12345&coupon_code=5dollarOff

This is great way to give customers the right links to buy a product(s) and get discount with a single url. This eliminates the hustle for the user to find the coupon field and apply it manually.

If you don’t want your query string in the url to be coupon_code, just change it in line 16 in the code at the beginning of this article.




Recent Articles

From Our Community Forums

Blog Categories

Related Articles

If you enjoyed reading this, then please explore our other articles below:

Comments

Leave a comment

Leave a Reply

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.