How to Create a Coupon Programmatically in WooCommerce
How to Create a Coupon Programmatically in WooCommerce
Creating promotional coupons programmatically on the WooCommerce store is easier than you think.
All you need to do is adding a few lines of code in the functions.php file in the theme. Here is a step by step procedure to walk you through the process
Open functions.php file in your theme.
The best way to open your functions.php is via the Theme Editor menu in the WordPress dashboard. It’s recommended to create a child theme instead of directly editing your theme to make changes on your website.
Go to the Appearance menu and choose the Editor option from the left sidebar.
On the theme editor screen, you can see the list of files on your theme on the right side of the screen. Click on functions.php file from the list to open it on the text editor at the center of the screen.
Adding the code snippet
Copy the following code snippet and paste it at the bottom of the functions.php file. If there is a closing PHP tag (?>) at the bottom, you need to paste the code above the closing PHP tag.
* Create a coupon programatically
$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$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 );
// Add meta
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, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
Create Coupon Name
Replace UNIQUECODE with a unique coupon code that buyers can use in your shop. Eg: CHRISTMAS30, NewYear20.
- Add discount value
Add the discount amount or percentage for the code without currency or percentage symbol.
- Choose discount type
Choose the type of discount for the product. You can choose from the 4 predefined values here: fixed_cart, percent, fixed_product, percent_product
fixed_cart : Fixed cart discount is used to define a fixed discount amount to the entire cart. If your discount value is 20 and the total cart value of a buyer is $200, the buyer will get a $20 discount and the final amount will be $180.
percent : It gives a fixed percentage discount for the entire cart. If your discount value is 10 and the total cart value is $300, the buyer will get a discount of 10% and the final amount after the discount would be $270.
fixed_product : This type of discount applies a fixed amount discount to each product in the cart. If the cart contains three t-shirts of $20 and the discount value is 20, the buyer will get a 20% discount for each item in the cart.
percent_discoount : It applies a percentage discount for each item in the cart.
In this example, we choose fixed_cart type. You can choose whatever type of discount you want based on your needs.
This is all you need to do to create a new coupon in WooCommerce programmatically. You can create multiple coupons like this using the code snippet again.
- Customizing the Coupon
Each line of code from the 21 to 27th line allows you to customize the coupon based on the unique needs.
Line 21: Individual use only
update_post_meta( $new_coupon_id, ‘individual_use’, ‘no’ );
This allows you to control if the coupon can be used in combination with other coupons. Changing the ‘no’ to ‘yes’ will allow buyers to use this coupon with another coupon, so they get multiple discounts. Most of the time, you may want to keep this as ‘no’.
Line 22: Applicable Products
update_post_meta( $new_coupon_id, ‘product_ids’, ” );
This line allows you to choose the products that must be in the cart in order for the discount to be applied by adding the product IDs inside the two single quotes. If you leave is blank, the coupon will be applied to all products.
Line 23: Exclude Products
update_post_meta( $new_coupon_id, ‘exclude_product_ids’, ” );
This line allows you to prevent the coupon from being applied for specific products. Sometimes, you may not want to give discounts for particular products. You can choose by adding their product IDs inside the two empty single quotes.
Line 24: Set usage limit for the coupon
update_post_meta( $new_coupon_id, ‘usage_limit’, ” );
It allows you to set the number of times the coupon can be used by all customers. You can put a desired number inside the two single quotes.
You can also set a usage limit for individual users instead of all users. For example, you can limit each user form using the code more than once by adding the following line of code.
update_post_meta( $new_coupon_id, ‘usage_limit_per_user’, ” );
Line 25: Set an expiry date for the coupon
update_post_meta( $new_coupon_id, ‘expiry_date’, ” );
This line allows you to set an expiry date for the coupon. You can leave it blank or set a date in the following format inside the two single quotes.
Line 26: Apply coupon before or after adding the tax
update_post_meta( $new_coupon_id, ‘apply_before_tax’, ‘yes’ );
This line of code allows you to decide if the coupon should be applied before or after the tax or before the tax is added to the total cost. You can change the ‘yes’ to ‘no’ to apply the coupon to the cost after adding the cost.
Line 27: Enable free shipping
update_post_meta( $new_coupon_id, ‘free_shipping’, ‘no’ );
You can change the ‘no’ to ‘yes’ if you want to offer the free shipping offer to the buyers who use this code.
That’s it. Get in touch if you want help setting this up on your site