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_code() and set_amount() are more than enough to create a coupon.

Create coupon programmatically WooCommerce
Marketing > Coupons in case you may forget.

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?

coupon configuration in WooCommerce

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( 
		'no-reply@rudrastyh.com', 
		'kate@rudrastyh.com',
	)
);


// 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 set_product_ids() and set_excluded_product_ids() methods.

Misha Rudrastyh

Misha Rudrastyh

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

Follow me on X