Search the site:

How to Apply Discount Coupon Automatically To WooCommerce Cart

Discounts are a great way to increase sales and helps to build a good customer relationship. It is a way to attract your customers to come back to your store and purchase it again.

WooCommerce also provides a feature to store owners for adding discount coupons for their customers. And they use different ways to reach out to their customers for the coupons like sending them emails, adding a banner on their website, and so on. But sometimes it is possible that the customers may forget to apply the coupon during the checkout.

So if as a store owner you do not want your customers to purchase products without using discount coupons, you have to find a way that the coupons are applied automatically to the cart when customers purchase. It can be done by providing coupon code URLs to customers via emails or add them on the website, which when clicked, the coupon will be applied automatically to the cart. Or the simpler way to achieve this is to apply the coupon code automatically when a customer adds a product to the cart.

In WooCommerce right now there is no option to apply the discount coupons automatically but it is possible to achieve this by adding a code snippet or using some of the extensions available.

Using a Code Snippet

Let us take an example here, where the store owner wants to give a discount of 20% for the orders valued more than $250 on the occasion of Halloween or Black Friday or Christmas and he wants that none of his customers should miss the discount. So to achieve this, there are 2 ways which we will be discussing below.

1. Applying coupon code directly to the cart when the product is added

For this, the first step is to create a coupon code in WooCommerce with a Minimum Spend Usage restriction to $250 under the Add New Coupon / Edit Coupon -> Usage Restrictions menu.

Minimum Spend for Coupon
Minimum Spend for Coupon

The coupon is added as per the owner’s requirements but as entering the coupon is still a manual process here, it is highly possible that the customer may forget to use the coupon code. So the best way to avoid this is to apply the code automatically to the cart. This can be achieved by adding the below code to the functions.php file of the child theme or using any code snippets plugin.

 * Apply Discount Coupon automatically to the cart 

function ts_apply_discount_to_cart() {
    $order_total = WC()->cart->get_subtotal();
    if( $order_total > 250 ) {
        $coupon_code = '20%OFF';
        if ( !WC()->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) {
add_action( 'woocommerce_before_cart_table', 'ts_apply_discount_to_cart' );

Here we have used woocommerce_before_cart_table hook which is in the WooCommerce cart form before displaying the cart table. In this hook, we are checking for the order total and if the order total is $250 or more, then we are applying the ‘20%OFF’ coupon to the cart using add_discount() function.

Automatically apply coupon code on cart
Automatically apply coupon code on the cart

2. Using Coupon Code URLs

Another way of applying coupon code to the cart directly is by providing the Coupon Code URLs to the customers by sending across emails, newsletters, or by posting them on the website in a banner. So when a customer clicks on the URL and if the product is already added to the cart, the coupon code will be applied automatically.

To achieve this, you need to add the below code in the functions.php file of your child theme.

 * Set session variable on page load if the query string has coupon_code variable.
function ts_get_custom_coupon_code_to_session() {

    if( isset( $_GET[ 'coupon_code' ] ) ) {
        // Ensure that customer session is started
        if( !WC()->session->has_session() )
        // Check and register coupon code in a custom session variable
        $coupon_code = WC()->session->get( 'coupon_code' );
        if( empty( $coupon_code ) && isset( $_GET[ 'coupon_code' ] ) ) {
            $coupon_code = esc_attr( $_GET[ 'coupon_code' ] );
            WC()->session->set( 'coupon_code', $coupon_code ); // Set the coupon code in session
add_action( 'init', 'ts_get_custom_coupon_code_to_session' );

 * Apply Coupon code to the cart if the session has coupon_code variable. 
function ts_apply_discount_to_cart() {
    // Set coupon code
    $coupon_code = WC()->session->get( 'coupon_code' );
    if ( ! empty( $coupon_code ) && ! WC()->cart->has_discount( $coupon_code ) ){
        WC()->cart->add_discount( $coupon_code ); // apply the coupon discount
        WC()->session->__unset( 'coupon_code' ); // remove coupon code from session
add_action( 'woocommerce_before_cart_table', 'ts_apply_discount_to_cart', 10, 0 );

Here we have used two hooks init and woocommerce_before_cart_table. Where in init hook we are checking if the current URL contains coupon_code query var or not and if it contains then the value of the variable is stored in the WooCommerce session.  In woocommerce_before_cart_table hook, we are checking that if the coupon_code variable is set in the WooCommerce session, then apply that coupon code to the cart.

After adding the above code you can use the below URL to send it to your customers.

Or if you want to redirect the URL directly to the cart or checkout page after applying coupon, you can use the below URLs.
Coupon Applied with URL contains coupon_code Variable
Coupon Applied with URL contains coupon_code Variable

Once the coupon is applied to the cart, you don’t want your customers to add another coupon to the same cart. So it is better to hide the coupon code field on the cart and checkout page. Here is another post that explains How to Hide the WooCommerce Coupon code field.

Using WooCommerce Extensions

Another way to achieve this is by using different WooCommerce extensions which allows you to apply the coupon code automatically to the cart. Below are some of the extensions:

  1. Smart Coupons – This plugin has everything you need to grow sales and customers using discounts, coupons, credits, vouchers, product giveaways, offers, and promotions. It’s the best-selling and most complete coupon management plugin for WooCommerce.
  2. WooCommerce URL Coupons – The WooCommerce URL Coupons extension helps you create unique URLs that automatically apply a discount and products to the customer’s shopping cart. You can change where the customer is redirected after visiting the unique URL, and your URL coupons will still obey all coupon restrictions, such as usage restrictions.
  3. Advanced Coupons – Advanced Coupons extends your coupon features so you can market your store better.
  4. WooCommerce Extended Coupon Features FREE – “WooCommerce Extended Coupon Features” adds functionality to the WooCommerce coupons and allows for automatic discount rules. Very easy to use, the functionality is conveniently integrated to the WooCommerce Edit Coupon panel.
  5. Auto Coupons for WooCommerce – It is a simple & lightweight plugin just allowing coupons to be applied automatically to the cart if the set rules meet.

So you can either use the code snippets or the plugins to offer the customer an unmissed discount for the purchases.

Browse more in: Code Snippets, WooCommerce, WooCommerce How Tos, WooCommerce Tutorials
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Inline Feedbacks
View all comments
4 months ago

This doesn’t check against the cart total – it’s checking against the cart sub-total – a bit misleading. I was looking for something that would let me auto-apply a coupon based on the cart total (after discounts applied)….

from purchased

Subscribe for more offers

Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins and 50% OFF on Bundles
Would love your thoughts, please comment.x