Notice: Function WP_Scripts::localize was called incorrectly. The $l10n parameter must be an array. To pass arbitrary data to scripts, use the wp_add_inline_script() function instead. Please see Debugging in WordPress for more information. (This message was added in version 5.7.0.) in /home/d689c45/public_html/wp-includes/functions.php on line 6078
How to Create WooCommerce Coupon Code natively or using PHP

WooCommerce coupon codes, just like any coupon from any content management system (CMS)  can help attract new customers and generate a lot of sales, as of 2020, almost 90% of consumers used coupons (Source: Statista). Therefore for every online store owner, coupons are of the utmost importance. Knowing when and how to use them can be just the push you need to own a million-dollar store.

 

How to create a WooCommerce Coupon Code natively 

WooCommerce makes it pretty easy to create and manage coupon codes. Coupons are a standard WooCommerce feature that allows store admins to generate a code that can be used by the customer to apply a discount on his order without using any external add-ons. The coupon could either be for applying a discount on a specific product on the entire cart.

Creating a WooCommerce Coupon Code that will apply discount on a specific product;

Step 1: In order to set up a percentage discount on products using coupons, kindly go to Marketing > Coupons from your store admin area. How to create WooCommerce coupon codes - Tutorial

Step 2: Click on the ‘Add Coupon’ button to access the coupons creation page

How to create WooCommerce coupon codes - Tutorial

Step 3: Enter your coupon code or click on ‘Generate coupon code’ to generate a random code for you. Keep in mind that all coupon codes must be unique.

Step 4: Please select Percentage discount as the discount type.

Step 5: Enter your percentage number in the coupon amount field.

Step 6: If you want to set an expiration date for the coupon, you can enter that date in the coupon expiry date field

Step 7: Click on Usage restriction to configure the coupon conditions for the customer to get the discount:

How to create WooCommerce coupon codes - Tutorial

  • Minimum spend: is the minimum to be spent by the customer in the current order to be eligible for the discount.
  • Maximum spend: is the maximum to be spent by the customer in the current order to be eligible for the discount.
  • Individual use only: to enable or disable the coupon usage along with other coupons for the same order.
  • Exclude sale items: excludes products that are already on sale from the coupon application.
  • Products: this allows you to list all the products on which the coupons will apply once enabled.
  • Exclude products: allows you to list all the products on which the coupons will apply once enabled
  • Product categories: allows you to list all the products on which the coupons will NOT apply once enabled
  • Exclude categories: list of categories that will be excluded from the coupon application
  • Allowed emails: customers billing emails or domains that should be able to use the coupon and get the discount applied

Step 8: Click on Usage limits to configure the coupon conditions for the customer to get the discount:

How to create WooCommerce coupon codes - TutorialUsage limit per coupon: allows you to set the number of times the coupon can be used

  • Limit usage to X items: allows you to set a maximum number of products per coupon usage
  • Usage limit per user: this allows you to set a maximum number of usage per customer. Each customer is identified by his billing email.

Once you’re done, click on the ‘Publish’ button to make your coupon live.

Creating a WooCommerce Coupon Code that will apply discount on the cart total

If you want the coupon to be applied on the entire cart instead of specific products, follow the above steps but, make sure the ‘products list’ to apply the discount on is empty. 

How to create WooCommerce coupon codes - Tutorial

That way, instead of applying the discount on a specific set of products, the coupon will apply the discount to the entire cart.

 

How to create a WooCommerce Coupon Code by using the programming language – PHP 

As a WordPress developer, you can use the PHP programming language to create coupon codes for your Woocommerce store or for your clients that have Woocommerce stores, simply because WordPress and Woocommerce run on PHP. To do so, you can use the code below: 

//Once you choose your coupon code you can enter it below
$coupon_code = 'UNIQUECODE2';

//Then you can set the amount that is fixed if you're setting a fixed discount. 
//If not, the number you set will be considered as a percentage.
$amount = '10'; // Amount

//Choose your discount type among these: fixed_cart, percent, fixed_product, percent_product
$discount_type = 'fixed_cart';

//Coupons are WordPress custom post types. So we can create them just like a regular custom post type
$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 );

//We make sure the coupon has been created so we can update it's properties
if ( $new_coupon_id ) {

    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' );
    
    //The list of products to which the coupon should apply is an array of the product id numbers
    update_post_meta( $new_coupon_id, 'product_ids', '' );
    
    //The list of products to exclude from the coupon application is an array of the product id numbers
    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' );

} else

echo "The coupon creation failed....";

There you have it, how to create your coupon codes natively with your WooCommerce or using PHP programming language. Other than using coupon codes, there are other ways of offering discounts to your customer and target audience in order to increase your sales, you can check here for a guide about discount types and how to set them up. 

Drop your thoughts or questions in the comment section, we are more than happy to listen and help!

Leave a comment

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