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.

Recommended Posts

40 Comments

  1. I am grateful that you have provided such helpful information. I haven’t been able to think of very many questions pertaining to this subject for some time. I’m going to stand by your side!

  2. Retro Bowl is a football game designed in the American manner by New Star Games. Are you prepared to lead your fantasy team to victory?

  3. A well-written piece that deals intelligently geometry dash lite and realistically with its subject.

  4. Thank you for sharing a very meaningful article, I think it will be very helpful for me and everyone. Play game Waffle online.

  5. Retro games are always available for free and feature vintage gameplay.

  6. The writing in this essay is superb. Your ideas have been structured in a way that makes them very clear and easy to understand. Retro bowl is the ideal synthesis of autoplay and control!

  7. Featuring vibrant and eye-catching visuals, these games offer a feast for the eyes while keeping players engrossed in their exciting gameplay. papa’s games

  8. i am not sure how to apply this coupon, luckily i saw your post, it helped me a lot. Thank you for sharing, have a nice day

  9. The news you share is very interesting, I and many others are interested in the news you share. fnaf security breach unblocked is a game for relaxing and having joy.

  10. We are the best Aluminum profile supplier or aluminium profile extrusion. We make sure to provide quality product to our clients


Add a Comment

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