Search

How to Apply Discount Coupon Automatically in WooCommerce

Listen to this article

Discount-Coupon-AutomaticallyIt’s Christmas and with Christmas comes offers galore. After all, discounts are a great way to increase sales, right? As a WooCommerce store owner, you too might have surely given discounts to your customers.

But, wait a minute. Sometimes customers don’t apply the coupon code to avail of the discount and you are left wondering why? Well, there could be only one reason for this to happen and that is that the customer did not notice the coupon code.

Now if you’re that person who doesn’t want customers to miss out on the discounts you might want a way to be able to apply the discount coupon automatically based on the offer on your WooCommerce store and I’m going to provide you with a solution for just that!

The following are two ways to apply a discount coupon automatically to a customer’s purchase:

1. Using a Plugin like ‘WooCommerce Auto Added Coupons’

  • WooCommerce Auto Added Coupons plugin allows users to create a coupon code in WooCommerce and then automatically apply it to the cart.
  • This option is provided to the user in the coupons tabs of WooCommerce in the dashboard.
  • The admin just needs to check a simple checkbox to apply the discount coupon automatically.
  • Let us understand this with an example. Suppose a 25% discount coupon has been set on cart level for a minimum order total of $100. Now, when the total order amount in the customer’s cart fulfills the coupon condition a 25% discount is automatically applied to the customer’s total order amount.

discount-coupon-automatically-plugin

  • This plugin is useful if you want to apply a discount coupon automatically at cart level or at product level for a certain minimum or maximum amount spent by the customer.

2. Programmatically Apply Discount Coupon to Customer’s Purchase

  • Let us assume a situation in which you might want to apply a discount coupon based on the collective weight of all the products in the cart or based on the address the product has to be shipped. In such a scenario the above plugin will not be useful.
  • In such a scenario you can programmatically apply the discount coupon to the order.
  • The first step in order to be able to do so would be to create a coupon in WooCommerce.
  • WooCommerce provides an option to create a coupon from the dashboard. However for the inquisitive minds out there you can use the following code provided by WooThemes to create the coupon code manually.
$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
                
$coupon = array(
    'post_title' => $coupon_code,
    'post_content' => '',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);
                    
$new_coupon_id = wp_insert_post( $coupon );
                    
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
  • Once the coupon code has been created it can then be used to apply discount coupon automatically using the following code.
//The folloing is the code to apply discount coupon automatically when the quantity of products in cart is greater than 10
add_action('woocommerce_before_cart_table', 'discount_when_quantity_greater_than_10');
function discount_when_quantity_greater_than_10 {
    global $woocommerce;
    global $total_qty;
    if( $total_qty > 10 ) {
        $coupon_code = 'UNIQUECODE';
        if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>The number of Product in your order is greater than 10 so a 10% Discount has been Applied!</strong></div>';
    }
}


Using the above code you can apply discount coupon automatically in various situations such as when the total weight of products in cart reaches a certain amount or when the shipping address is within a certain area. The applications are many but the code remains the same in all situations. Of course, you gotta put your own conditions! 😉

So, that was about how to apply discount coupon automatically. If you have anything you would like to ask or add please feel free to use the comments section. Until next time adios guys 🙂

Tahseen Kazi

Tahseen Kazi

16 Responses

  1. My website has a sign up form where visitors can put in their name, email and phone number to sign up.
    I need a feature where, as soon as the visitor signs up, an email is sent to his/her email address containing a unique coupon code. This code can then be used on the user’s next order.

    The coupon needs to:
    – Have a unique code
    – Usable only once
    – Be subject to certain cart conditions such as minimum order
    – Have an expiry specified number of days from sign up (for example, 7 days from sign up)

    1. Hi. The plugin Follow-Up Emails is great for this task. You can create emails to be sent automatically when the customer signs up, or at the first purchase, or at the next purchases, etc… and you can generate unique coupon codes for each client under many configurable situations.

    1. Hi Sumanta,

      You’ll have to register to the ‘user_register’ hook and create a new coupon using the code mentioned in the article.

  2. This looks exactly like what I need. Can you please tell me where (in what .php file) do I put the second code?

    1. Sean, you can add the code in two ways
      1)You can add the code in the functions.php file of your current theme and you will be able to achieve the functionality.
      2)Alternatively you can create a plug-in and add it to the plugins folder which is present in the wp-content folder.
      I would recommend that you use the second method, as, in the first method the code will no longer be available to you if you change your theme or upgrade your theme.

  3. You put this comment on top of the second code –
    //The folloing is the code to apply discount coupon automatically when the quantity of products in cart is greater than 10

    And then checked to see if the total weight is greater than 100???
    if( $total_weight > 100 ) {

  4. By Using Discounts for Next Orders Plugin. We can create coupons automatically for orders and pull the customer to next purchase.

    And Auto applying feature is also available.

  5. //The folloing is the code to apply discount coupon automatically when the quantity of products in cart is greater than 10
    add_action(‘woocommerce_before_cart_table’, ‘discount_when_quantity_greater_than_10’);
    function discount_when_quantity_greater_than_10 {
    global $woocommerce;
    global $total_qty;
    if( $total_qty > 10 ) {
    $coupon_code = ‘UNIQUECODE’;
    if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
    $woocommerce->show_messages();
    }
    echo ‘The number of Product in your order is greater than 10 so a 10% Discount has been Applied!‘;
    }
    }

    Code is not working when i added the code in my functions.php

  6. I use mailerlite for building my email list. Is there a way that, when a user signs up on the email form (let’s say email pop up) a coupon is automatically applied to their cart? Don’t want users leaving the page to check their email for the code.

  7. Hi there,

    I would like to add a fixed amount discount on shipping when customers bought over a certain amount. I have various shipping zones, so this shipping discount only needs to apply to one of them. How do I do this?

    Thank you!!

  8. Hey, thanks for sharing! Quick note with a couple code updates.

    This:
    $woocommerce->show_messages();

    Should be replaced with:
    wc_print_notices();

    And:
    add_discount

    Should now be:
    apply_coupon

  9. Hi everyone, im giving a 10% discount every Tuesday on my website shop. I’m looking at applying automatic coupon every time a customer chooses Tuesday as their preferred delivery day. Is it possible to write up a code for it?

  10. I’ve been trying to get this to work, but without success.
    Both notices are being displayed when I call them, so i know for sure code is running.
    But only removing a coupon works.
    Changing $cart->apply_coupon to $cart->add_discount doesn’t work either.
    What am I missing here?

    if ( $cart->has_discount( $coupon_code4 ) && ! $test_ia5) {
    $cart->remove_coupon( $coupon_code4 );
    wc_clear_notices();
    wc_add_notice( __(“remove_coupon”,”woocommerce”), ‘notice’);
    }
    if ( ! $cart->has_discount( $coupon_code4 ) && $test_ia5 ) {
    $cart->apply_coupon( sanitize_text_field($coupon_code4));
    wc_clear_notices();
    wc_add_notice( __(“apply_coupon”,”woocommerce”), ‘notice’);
    }

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads

    WordPress Tips & Tricks
    Ketan Vyawahare

    How to Make Responsive Tables using CSS without Tag Read More »