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.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

How to Add Custom Coupon Code in Woocommerce

Updated on November 4, 2021

6 Min Read
woocommerce coupon code

Many people think WooCommerce coupons are a thing of the past – something that old people 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 receive.

In this article, I am going to educate you on how to create custom coupons in WooCommerce. Besides that, we will also discuss some of the best WooCommerce coupon code plugins.

So without further ado, let’s dive into the article.

Table of Contents

Best WooCommerce Coupon Plugins

Here are some of the best WooCommerce coupon plugins in 2020 that can help you acquire a lot of customers and grow sales.

1. Coupon Creator

Coupon Creator

This plugin is one of the best WooCommerce coupon plugins that allows you to create your own coupon with the Coupon Creator for WordPress or upload an image of a coupon instead. The plugin has 10,000+ active installations.


  • Create coupons with an expiry date
  • Customer can get the coupon printed
  • Modern Advanced Template
  • Dynamic code feature available
  • Disable the Print View per coupon

2. WooCommerce Smart Coupons

WooCommerce Smart Coupons

By using this plugin, customers can easily buy credits for themselves or gift them to others. You can even manage everything related to coupons, credits, and gift certificates. This is a paid plugin that you can get from WooCommerce official website.


  • Create a customized coupon design
  • Allows sharing coupons URL
  • Allows coupons to pay for subscriptions
  • Set credit balance and coupons automatically
  • Allow one-click coupon use

3. Coupon Generator for WooCommerce

Coupon Generator for WooCommerce

This plugin allows you to generate thousands of coupons code at a time. The best part of this plugin is that a coupon code is generated in an effective way and they even work on low-cost hosting. Coupon Generator has 8,000+ active installations.


  • Create an unlimited number of coupons
  • It helps you to bring more customers
  • Easy to setup and integrate
  • Get daily progress updates
  • Up to 1,000,000 coupons tested

Above were the top WooCommerce promo code on my list. Let’s move to the second step which is a custom coupon.

Custom coupon for WooCommerce

Adding a custom WooCommerce coupon code 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 coupons through a 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 a discount coupon code, and then I’ll show you the impact of the coupon in action.

For a better understanding, I have broken it all down in a few steps below.

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 coupon has been created, it will be applied automatically 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 the 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 a comma in the array.

For reference, the Product ID can be found under the product name in All Products listing.

Product ID

Step 2: Update WooCommerce Coupon Information

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.

Update WooCommerce Coupon Information

You have the option to change the discount type for the product, along with the discounted amount. Since 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 such 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 Cloudways coupon code 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 you set its information through the WooCommerce menu. The good news is that power users can 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 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 use a 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' );

Generate WooCommerce Coupon through Code

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

Frequently Asked Question

 Q: What is a WooCommerce coupon code?

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: Are WooCommerce coupon codes case sensitive?

Yes, WooCommerce coupon codes are case sensitive. Enter your coupon codes carefully to avail discounts on any of the products or services from a WooCommerce store.

 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 the coupon in WooCommerce?

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

Wrapping Up!

Creating coupons for a WooCommerce store is a simple process with great potential for increasing store sales and profit margins. In this tutorial, I have discussed several methods of adding coupons to your online store.

If you have any comments regarding this tutorial, feel free to share them via the comments section below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

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 [email protected]


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

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now