How to Create & Add Custom Woocommerce Coupon Code

Many people think that WooCommerce coupons are a thing of the past - something that old people used to clipping newspapers to get 25-percent off at local stores. However, as many eCommerce store owners will tell you, coupons are an important source of revenue, simply due to savers.

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

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

Best WooCommerce Coupon Plugins

Here are some of the best WooCommerce coupon plugins in 2020 that can help you gain lots of customers and increase sales.

1. Coupon Maker

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

Features:

  • Create coupon with expiry date
  • Customer can get a printed coupon
  • Modern advanced template
  • Dynamic Code Facility Available
  • Disable print view per coupon

2. WooCommerce Smart Coupons

Using this plugin, customers can easily purchase credits for themselves or gift them to others. You can also manage everything related to coupons, credits, and gift certificates. This is a paid plugin that you can get from the official website of WooCommerce.

Features:

  • Create a customized coupon design
  • Coupon URL allows sharing
  • Gives coupons to pay for membership
  • Automatically set credit balance and coupons
  • Allow one-click coupon usage

3. Coupon Generator for WooCommerce

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

Features:

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

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

Custom Coupons for WooCommerce

Adding a custom WooCommerce coupon code is not really a simple method. Although there are many ways in which WooCommerce store owners can add and offer coupons at their stores, the method provides the most control over the entire process of creating and creating coupons through a code.

This process is straightforward and involves adding the 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 function.php of the child theme.

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

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

Step 1: Add a WooCommerce coupon code for a specific product

To create a coupon with a specific title (I chose Cloudways WooCommerce Hosting for the purpose of this tutorial), add the following code to the function of the child theme.

Note that after the coupon is created, it will be automatically applied for the specific product ID.

add_action( 'woocommerce_before_cart', 'cw_coupons_matched' );
function cw_coupons_matched() {
    global $woocommerce;
    $cw_coupon = 'codelanguage';
    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 );
            wc_print_notices();
        }
    }
}

In the above snippet, notice the line:

$autocoupon = array( 65 )

This is the product ID for which the coupon codelanguage is automatically applied. If you want to apply coupons for multiple products, enter a comma-separated ID in your array.

For reference, the product ID can be found under the product name in the product list.

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 coupon menu and select the coupon 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 the discount type, coupon amount and coupon expiration date.

You have the option to change the discount type for the product with the discount amount. Since coupons are time-limited, you should be careful in setting expiry dates to ensure that the coupon expiration matches the period stated in marketing campaigns.

Step 3: WooCommerce coupon code in action

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

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

Step 4: Apply material to all products

There are cases when the store owner wants to create a coupon that applies to all products at the store. Many popular examples include a holiday season discount or a store-wide marketing campaign.

Here's how you can make such coupons.

Note: With all the code mentioned in this tutorial, it will be added to the functions.php of the child theme.

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 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 snippet above, I created a coupon with all the desired parameters and then put it in the list of active coupons.

Generate WooCommerce Coupons via Code

You can easily generate a WooCommerce coupon code through the following snippet. Note that you can either use a snippet in the WooCommerce coupon plugin or as part of a currently active 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' );

Comments