Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Create Custom Woocommerce Coupon Codes

June 27, 2019

5 Min Read
woocommerce coupon code
Reading Time: 5 minutes

Many people think WooCommerce  coupons are a thing of past – something that old ladies used to clip off newspapers to get a 25-cent discount at the local store. However, as many ecommerce store owners would tell you, coupons are an important source of revenue, simply because of the savings that the shoppers perceive.

However, adding a custom WooCommerce coupon codes is not really a simple process. Although there are several ways in which WooCommerce store owners can add and offer coupons at their store, the method that offers the most control over the entire process of creating and offering is by creating the WooCommerce coupons through code.

This process is straightforward and involves adding WooCommerce coupon code to the functions.php file. At this point, it is important to point out that all the code mentioned in this tutorial should be added to the child theme’s functions.php.

I will start by demonstrating how to create a WooCommerce coupon code. Next, I will show you how to add the discount coupon code, and then I will show the impact of the coupon in action.

Step 1: Add WooCommerce Coupons Code for a Specific Product

To create a coupon with a specific title (I have chosen Cloudways WooCommerce hosting for the purpose of this tutorial), add the following code to the child theme’s functions.php.  Note that after the WooCommerce coupon has been created, it will be automatically applied for specific product IDs.

add_action( 'woocommerce_before_cart', 'cw_coupons_matched' );

function cw_coupons_matched() {

    global $woocommerce;

    $cw_coupon = 'cloudways';

    if ( $woocommerce->cart->has_discount( $cw_coupon ) ) return;

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {

        $autocoupon = array( 65 );

        if( in_array( $values['product_id'], $autocoupon ) ) {
            $woocommerce->cart->add_discount( $cw_coupon );

In the above snippet, notice the line:

$autocoupon = array( 65 )

This is product ID for which the coupon Cloudways is applied automatically. If you wish to apply the coupon for multiple products, just enter their ID separated by comma in the array.  For your reference, Product ID could be found right under the product name in the All Products listing.

woocommerce discount Product ID

Step 2: Update WooCommerce Coupon Information

Once you have the WooCommerce coupon, the next step is to add it to update the rules and information for the coupon. For this go to the Coupons menu and select the coupon that you have created.

Since I have already created Cloudways, I will update the data for the coupon. The most important items on this page are Discount Type, Coupon Amount, and the Coupon Expiry Date.

coupon data

You have the option to change the discount type for the product, along with the discounted amount. Since WooCommerce coupons are time-limited, you should be careful in setting up the expiry date to ensure that the coupon expiry matches the duration promised in the marketing campaigns.

Step 3: The WooCommerce Coupon Code in Action

At this point, the coupon (Cloudways in my case) is ready for business.

As you can see from the following screenshot of the cart page, a discount of £10 is automatically applied to the invoice.

cart total

Step 4: Apply Content for all Products

There are cases when the store owner wishes to create a coupon that is applicable to all products on the store. Several popular examples include a Holiday Season discount or a store-wide marketing campaign. Here’s how you can create these coupons.

Note: As with all the code mentioned in this tutorial, this will be added to the child theme’s functions.php file.

add_action( 'woocommerce_before_cart', 'cw_coupons_matched' );

function cw_coupons_matched() {
    global $woocommerce;
    $cw_coupon  = 'cloudways';
    if ( $woocommerce->cart->has_discount( $cw_coupon  ) ) return;
    $woocommerce->cart->add_discount( $cw_coupon  );

As you can see from the code snippet, the coupon code Cloudways is applicable on all products on the store that accept coupon codes.

Step 5: Create Discount Coupon Through Code

So far you have created the coupon first and then set it’s information through the WooCommerce menu. The good news is that power users could create and set the coupon information through a simple code snippet.

$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 );

As you can see, in the above snippet, I have created a WooCommerce coupon with all the desired parameters, and then inserted it into the list of active coupons.

Generate WooCommerce Coupon through Code

You can easily generate WooCommerce coupon codes through the following snippet. Note that you can either used snippet in a WooCommerce coupon plugin OR as part of the currently activated theme (in the functions.php file)

$coupon_code = 'WooCommerce Coupons'; // Code
$amount = '05';
$discount_type = 'fixed_cart';
$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 );
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, 'free_shipping', 'no' );

For further reference, check out the official WooCommerce coupon code  documentation

Frequently Asked Questions

 Q. Are WooCommerce coupon codes case sensitive?

Yes, WooCommerce coupon codes are case sensitive. So enter your coupon codes carefully to avail discounts on any of the product or service from a WooCommerce store.

 Q. What is a WooCommerce coupon code?

A WooCommerce coupon code is an effective marketing tool that is used to convey a particular promotion, a discount, a refund (also called cashback) or a benefit between ecommerce store and customer.

 Q. How does a promo code work?

The text box in which you enter the code is normally mentioned as a promotional code, discount code, coupon, voucher or discount voucher. Once the discount code has been applied, the amount appears updated directly in the online store cart.

 Q. How to show WooCommerce coupon?

You can show your coupon code by using WooCommerce Coupon Shortcodes plugin. This extension for WooCommerce allows you to render coupon information and show content based on the validity of coupons.


Creating coupons for WooCommerce store is a simple process with great potential for increase the store sales and profit margins, In this tutorial, I have discussed several methods of adding coupons to the store. If you need help with the process, just leave a message and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Your WooCommerce Store Needs A Reliable Host

With no compromise on Performance, Security & Support.

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019