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) {

	 * 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])) {

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


	// Apply the coupon to the cart if necessary.

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

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

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.

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

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:

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

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


Leave a comment
  1. BJ April 23, 2021 at 2:19 am - Reply

    Thank you!

  2. Joel September 29, 2020 at 1:58 pm - Reply

    Is it possible to add multiple coupons in the URL, and have the appropriate coupon be honoured, when they reach the milestone.

    For example: Coupon 1 = Buy 1 get 10% off
    Coupon 2 = Buy 2, get 15% off
    Coupon 3 = Buy 3 or more, get 20% off

    I’m going the coupon route, because I don’t want these settings to be applied site wide, but instead, only on specific landing pages.


  3. constantino garcia azuara August 12, 2020 at 8:24 pm - Reply

    It works perfect! tnks!

  4. Ivan August 8, 2020 at 5:39 pm - Reply

    I really appreciate your tutorial. Thank you very much. It works perfect for me :)

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.