Create Coupons Programmatically
The inspiration for this tutorial was another tutorial I found where it was recommended to create WooCommerce coupons using
wp_insert_post() function and
update_post_meta(). When working with WooCommerce data since 3.0 version it is always recommended to use CRUD objects. And coupons are not an exception.
In order to create a coupon programmatically in code we have to use
WC_Coupon object. Once the object is created we are able to use its methods for coupon configuration.
Let’s do it and begin with something simple.
$coupon = new WC_Coupon(); $coupon->set_code( 'blackfrd' ); // Coupon code $coupon->set_amount( 200 ); // Discount amount $coupon->save();
As you could’ve guessed, two methods
set_amount() are more than enough to create a coupon.
As simple as that. The only thing you have to double check before using this code is that WooCommerce is actually loaded, otherwise you will get Fatal error: Uncaught Error: Class ‘WC_Coupon’ not found in blah blah. Just consider using
plugins_loaded action hook if you’re going to create a coupon in your custom plugin.
With that – done! What about some coupon configuration now?
Let’s create another coupon right now and configure everything like on the screenshot.
/** * Create a Coupon Programmatically * * @author Misha Rudrastyh * @url https://rudrastyh.com/woocommerce/create-coupon-programmatically.html */ $coupon = new WC_Coupon(); $coupon->set_code( 'ny_eve_2022' ); $coupon->set_description( 'Some coupon description.' ); // General tab // discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart' $coupon->set_discount_type( 'percent' ); // discount amount $coupon->set_amount( 20 ); // allow free shipping $coupon->set_free_shipping( true ); // coupon expiry date $coupon->set_date_expires( '31-12-2022' ); // Usage Restriction // minimum spend $coupon->set_minimum_amount( 1000 ); // maximum spend $coupon->set_maximum_amount( 50000 ); // individual use only $coupon->set_individual_use( true ); // exclude sale items $coupon->set_exclude_sale_items( true ); // products $coupon->set_product_ids( array( 132 ) ); // exclude products $coupon->set_excluded_product_ids( array( 15, 16 ) ); // categories $coupon->set_product_categories( array( 17 ) ); // exclude categories $coupon->set_excluded_product_categories( array( 19, 20 ) ); // allowed emails $coupon->set_email_restrictions( array( 'email@example.com', 'firstname.lastname@example.org', ) ); // Usage limit tab // usage limit per coupon $coupon->set_usage_limit( 100 ); // limit usage to X items $coupon->set_limit_usage_to_x_items( 10 ); // usage limit per user $coupon->set_usage_limit_per_user( 2 ); $coupon->save();
The only thing I would like to highlight here is that you can also pass variation IDs in
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
You are my hero :-) I created php script for bulk generating coupons, now I’m writing wp-cli command for it.
Hello, Misha! First of all, thanks for your blog.
I added this code with Code Snippets Plugin and it generated a critical error. I solved it with the safe mode: https://DOMAIN.com/wp-admin/admin.php?page=snippets&snippets-safe-mode=true and then eliminated.
Do you know why this code generates that error? I tried the simple version of only 4 lines and it happens again.
It is because this code is not just for
Why is not for functions.php? If I create a plugin, it will work, right?
Absolutely great, thank you
Thanks, I was using the wp_insert_post method before, but this is better and easier.