Skip to content
Apply-WooCommerce -Coupons

How to Apply WooCommerce Coupons Programmatically

As you probably know, WooCommerce coupons allow you to apply discounts to user orders on the cart and checkout pages of your website.

In this tutorial, I am going to show you how to apply your WooCommerce coupons for a variety of conditions using code snippets.

This tutorial works with the Jupiter X or any other theme you are using for your website. In the following examples, we will be using the JupiterX theme.

Check if coupons are already applied

When applying a coupon using code, we should check that the coupon is not applied already.

To achieve this, we just need to use the following WooCommerce native function:

$coupon_code = 'couponToApply';


if ( ! WC()->cart->has_discount( $coupon_code ) ) {
    // rest of code
}

As you can see, here we’ve created a WooCommerce coupon from the dashboard with the coupon code “couponToApply”.

In the code snippet we are using the has_discount function to check if this coupon has already been applied or not. If it has not been applied, we will apply it using following method:

$coupon_code = 'couponToApply';


if ( ! WC()->cart->has_discount( $coupon_code ) ) {
    // rest of code
    WC()->cart->apply_coupon( $coupon_code );
}

We can apply any WooCommerce coupons using the apply_coupon function.

In order to use this snippet correct, we should attach it to the proper WooCommerce hook. 

The best way to test or use this snippet is to add it to your theme’s functions.php file.

Apply WooCommerce coupons without conditions

Of course, when applying a coupon there will always be at least one condition: The coupon should not be applied already.

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';


    if ( ! WC()->cart->has_discount( $coupon_code ) ) {
        // rest of code
        WC()->cart->apply_coupon( $coupon_code );
    }
}

The above hook (‘woocommerce_before_cart’’) is the best way to apply our coupons using code snippets because it is applied before our clients see the cart content, meaning that it will be viewable via the cart page.

After adding this snippet to our theme, we can add some products to our cart, and this will be the result:

Apply WooCommerce Coupons

Apply WooCommerce coupons on the checkout page

We can apply coupons on both the cart and checkout pages.

For now, let’s apply coupons on the checkout page. We need to expand conditions a little more to achieve this, as you can see in the following snippet:

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';


    if ( ! is_checkout() ) {
        return;
    }


    if ( ! WC()->cart->has_discount( $coupon_code ) ) {
       return;
    }


    WC()->cart->apply_coupon( $coupon_code );
}

Here we have added another condition that cancels the process if it is not the checkout page.

Apply coupons if a specific product is added to the cart

Sometimes you need to apply WooCommerce coupons if certain products are added to the cart and remove those coupons if those products are removed from the cart.

Let’s do this with the following snippet:

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';


    // Apply coupon if the product with this id is added to cart
    $product_id = 25;


    // Products that are added to cart.
    $products = WC()->cart->get_cart();


    if ( in_array( $product_id, array_column( $products, 'product_id' ), true ) ) {
        WC()->cart->apply_coupon( $coupon_code );
    } else {
        // Remove coupon if user has removed this product from cart.
        WC()->cart->remove_coupon( $coupon_code );
        WC()->cart->calculate_totals();
    }
}

Apply WooCommerce coupons based on the number of products in the cart 

Sometimes you need to apply coupons when a certain number of products have been added to the cart.
You can achieve this using the following snippet:

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';


    // Products that are added to cart.
    $products = WC()->cart->get_cart();


    // If more than 2 unique products are added to cart apply coupon else remove it.
    if ( count( $products ) > 2 ) {
        WC()->cart->apply_coupon( $coupon_code );
    } else {
        // Remove coupon if user has removed this product from cart.
        WC()->cart->remove_coupon( $coupon_code );
        WC()->cart->calculate_totals();
    }
}

If more than two unique products are added to the cart, the coupon will be applied. If the user removes products and the number of unique products in the cart falls below two2, the coupon will be removed.

Apply coupons based on cart subtotals

It is a great idea to apply coupons when the cart subtotal is greater than a certain amount:

The following snippet will apply the coupon if the cart subtotal is greater than $20. It will also remove the coupon if the cart subtotal drops below $20.

If you want, you can change the $20 limit and apply any other conditions as desired.

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';


    // If more than 2 unique products are added to cart apply coupon else remove it.
    if ( 20 < WC()->cart->get_subtotal() ) {
        WC()->cart->apply_coupon( $coupon_code );
    } else {
        // Remove coupon if user has removed this product from cart.
        WC()->cart->remove_coupon( $coupon_code );
        WC()->cart->calculate_totals();
    }
}

Apply coupons for a certain user

We can decide to apply a coupon for a certain user or group of users using the following snippet:

add_action( 'woocommerce_before_cart', 'apply_woocommerce_coupon' );


function apply_woocommerce_coupon() {
    $coupon_code = 'couponToApply';
    $users_id = [ 2 ];


    $current_user = get_current_user_id();
   
    if ( ! in_array( $current_user, $users_id, true ) ) {
        return;
    }


    WC()->cart->apply_coupon( $coupon_code );
   
}

In this example, we defined an array of users. If the current user ID exists in the defined array, the coupon is applied. 

Conclusion

As you can see, you can easily apply WooCommerce coupons programmatically based on a huge variety of conditions. You can also remove coupons if certain conditions are met.


If you are looking for an easier way to apply coupons, try SellKit, one of the best plugins for personalizing WooCommerce promotions on the market, which allows you to automatically apply discounts and coupons to your WooCommerce store pages without writing a single line of code.

Create Your
Dream Website with

Stay in the Loop

Sign up for our newsletter and stay up-to-date on the
latest WordPress trends, insights, and resources.

By entering your email, you agree to our Privacy policy and Terms of Services.

Share

Hadi Mohammade

Hadi Mohammade

I am a web artisan with more than 6 years of experience and am really interested to share my knowledge with everyone out there. Alongside programming I am a professional gamer, playing some online video games with friends after work is the best of fun for me.

3 Comments

  1. Be sure to place this code in a location where it will be executed when necessary, such as a custom WordPress plugin or theme functions file. Also, make sure that woocommerce is set up and configured correctly with the coupons that you want to apply programmatically. It’s a useful blog, I like it.

  2. great

  3. great


Add a Comment

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