Apply Coupons on Order for WooCommerce

Customer Rating
4.2/5

In the WordPress dashboard, go to WooCommerce > Coupons and click Add coupon. In some WooCommerce versions, you may find it under Marketing > Coupons. Then enter a coupon code name. You can either type any name you want or use an auto-generated name.

Description

Do you want to create coupons and add discounts to increase customer engagement? In this guide, we’ll show you how to create and apply a coupon in WooCommerce programmatically. We’ll show you different examples of what you can do to increase your sales.

Why use discount coupons in WooCommerce?

Most eCommerce sites have conversion rates that rarely exceed 3% so store owners are always looking for ways to increase their sales. One of the most effective ways to achieve that is by using discount coupons.

Discounts can provide users with an extra benefit that can help them make up their minds and buy your product. For example, you could use coupons for important eCommerce dates such as Christmas or Black Friday, offer discounts for a limited time, create coupons for your most loyal customers, and so on. There are many ways in which offering coupons can help increase conversion rates and boost your sales.

That’s why in this guide, we’ll show you how to create and apply coupons in WooCommerce programmatically.

How to create and apply a coupon in WooCommerce programmatically

In this tutorial, you will learn how to create and add WooCommerce coupons in different ways:

  1. Create WooCommerce coupons
  2. Apply coupon to:
    1. All products
    2. Specific products by ID
    3. The subtotal cart value
    4. The total cart depending on the number of products

As you will edit the functions.php file of the child theme, before you start you need to install a child theme. You can either create one or use a child theme plugin. Additionally, keep in mind that we’ll use several WooCommerce hooks so, if you’re not familiar with them, we recommend you have a look at this post.

1) Create coupons in WooCommerce settings

Before applying coupons, you need to create them. So, the first step is to create a coupon in WooCommerce. Using the inbuilt WooCommerce feature you can easily create them in a few clicks.

In the WordPress dashboard, go to WooCommerce > Coupons and click Add coupon. In some WooCommerce versions, you may find it under Marketing > Coupons. Then enter a coupon code name. You can either type any name you want or use an auto-generated name. In this case, we’ll name it auto_coupon.

Then, you have to choose the discount type (percentage, fixed cart discount, or fixed product discount), the amount (percentage or fixed amount), and the expiry date. You can also set restrictions terms of the products that the coupon applies for, the minimum amount that customers need to spend to apply the coupon, limit the usage per coupon or user, and many others.

After you’ve set up your coupon, press Publish.Create woocommerce couponIt’s worth noting that you can change any of these settings at any point. Just be aware that if you do it, you’ll need to adjust the scripts we’re going to use to match the new coupon code’s settings.

If you want more details on how to create and set up coupons in WooCommerce, you can check out this step-by-step guide.

Now you know how to create coupons in WooCommerce. Let’s have a look at how to apply them.

2.1) Apply coupon programmatically to any product

First, let’s see how to apply a coupon to any WooCommerce product. For this, we’ll use the woocommerce_before_cart hook to run the script before the cart page loads. Additionally, we’ll work with the WooCommerce cart object to retrieve the information we need to run our validation before applying the coupon code we’ve just created (auto_coupon).

The WC()->cart object is available in front-end pages and it allows you to retrieve all the information related to the cart page. So paste the following script in the functions.php file of your child theme:

add_action( 'woocommerce_before_cart', 'QuadLayers_apply_coupon' );
    function QuadLayers_apply_coupon() {
        $coupon_code = 'auto_coupon';
        if ( WC()->cart->has_discount( $coupon_code ) ) return;
        WC()->cart->apply_coupon( $coupon_code );
        wc_print_notices();
    }

As you can see, we are using an if() conditional to check if the coupon has been already applied. To know that, you retrieve the WooCommerce cart object like this: WC()->cart->has_discount($coupon_code);

It’s important to note that you’ll need this kind of validation in every single case. If you don’t do this the right way, you will have the coupon added multiple times, or when the cart page is reloaded. Also, note that WC()->apply_coupon($coupon_code) is how you apply the defined coupon to the current cart page.

2.3) Apply WooCommerce coupons to specific product IDs

Another interesting option is to apply a coupon to specific WooCommerce product IDs. To do that, you can use the following script. Just make sure you edit the $products_id() array to match your product IDs.

add_action( 'woocommerce_before_cart', 'QuadLayers_apply_matched_id_products' );
function QuadLayers_apply_matched_id_products() {  
    
    // previously created coupon;
    $coupon_code = 'auto_coupon'; 
    // this is your product ID/s array  
    $product_ids = array( 664,624,619 ); 
    // Apply coupon. Default is false
    $apply=false;
 
    // added to cart products loop
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {                      
        if ( in_array( $cart_item['product_id'], $product_ids )):              
            // Id of product match
            $apply=true;
            break;                     
        endif;  
    }
    // apply & remove coupon
    if($apply==true):
        WC()->cart->apply_coupon( $coupon_code );
        wc_print_notices();  
    else:    
        WC()->cart->remove_coupons( sanitize_text_field( $coupon_code ));
        wc_print_notices();   
        WC()->cart->calculate_totals();    
    endif; 
}

Here, we’ve used a foreach() loop to loop over all the products that were added to the cart. If any of them matches with the specified IDs, the $apply variable is set to true. This way, the validation simply applies the coupon when the variable is true. Otherwise, it removes the coupon.

2.4) Apply coupon to the subtotal cart value

Now, let’s say that instead of adding discounts based on the products, you want to apply a coupon in WooCommerce when customers spend more than $50 in your store. This is a great way to reward your shoppers and encourage them to spend more money on your site. So, with the following function, you can apply a coupon only when the cart subtotal value is more than 50.

add_action('woocommerce_before_cart','QuadLayers_apply_coupon_cart_values');
function QuadLayers_apply_coupon_cart_values(){

Reviews (0)

Reviews

There are no reviews yet.

Be the first to review “Apply Coupons on Order for WooCommerce”

Your email address will not be published.

Item Information

  • Item Version
    v2.2
  • Item Type
    New
  • Licence type:
    GPL
  • Automatic Updates:
    Yes
  • Item Code:
    UG998
  • Uploaded By
    Pitter
GET ANY ACCESS JOIN CLUB TO UNLOCK THIS DATA

1. All digital products are the most recent version, with no possibility of free updates. After payment, you can request an update to the most recent version for 7 days if a new version is released.

2. After the purchase is confirmed, download links available on My Account. If a license is required, please contact us via email or Live chat assistance with activation. Our license is only valid for activation and does not include support.

3. We provide PHP script installation services for $15. Please create a backup after installation as we do not support re-installation. For mobile app source code, we do not offer installation services. For Installation service Click Here

4. If you have any questions, please contact us by email support@fiwazo.com or create a Live Chat on this page

5. Please note that any digital products presented on the website do not contain malicious code, viruses or advertising. You will receive the original files from the developers. We do not sell any products that have been downloaded from other websites.

6. The response time can last up to 6 hours.

Join to club

Start from $0.1 / Month

Different instant download and backorders on Maxkinon Marketplace

Different instant download and backorders. A backorder is any item that requires another order because it can't be fulfilled. When the item is available for Download you get a notification for free download.

Changelog

Data not submitted by Uploader