Bulk add WooCommerce Coupons without a plugin

I found myself in a situation recently where I needed to bulk create unique coupons tied to specific people’s names, all with the same coupon conditions (fixed cart discount, attached to specific products, only allowed to be used once). The list of names was coming in on a rolling basis over the course of a few days.

However, I couldn’t justify buying a plugin like Smart Coupons for a single event. So I decided to write a quick function that will take a list of names and create coupons from it. This function is safe, so you can leave it active, but ideally once you’ve used it to create the coupons, you should remove the function for performance reasons.

This isn’t the best solution in the world, but it works in a pinch.

The code is heavily commented. Just drop it into your functions.php and modify as needed. 🙂

function aurooba_makes_bulk_add_woo_coupons() {
	// If we're not in the Dashboard, don't execute
	if ( ! is_admin() ) {
	// Initializing a few empty arrays we'll need.
	$coupon_codes_lower = array();
	$coupon_names       = array();

	// Here's where you add each name or phrase you want to make a coupon out of.
	$coupon_codes = array(
		'Aurooba Ahmed',
		'Anne Shirley',
		'Mohammad Ali',
		'Jane Smith',
		'Ender Wiggin',
		// Add as many as you want

	// WooCommerce converts them to lowercase anyway (odd but roll with it), 
	// so we're doing it here to make comparison simpler later in the function.
	foreach ( $coupon_codes as $code ) {
		$coupon_codes_lower[] = strtolower( $code );

	// Since all the coupons have the same settings, I'm setting them up here.
	$discount_type = 'fixed_cart';
	$coupon_amount = 15;

	// Let's grab all the existing coupons, so we don't re-add an existing coupon
	$args             = array(
		'posts_per_page' => -1,
		'orderby'        => 'title',
		'order'          => 'asc',
		'post_type'      => 'shop_coupon',
		'post_status'    => 'publish',
	$existing_coupons = new WP_Query( $args );

	// We only need the coupon names, so let's pull those out and store them in a list.
	foreach ( $existing_coupons->posts as $coupon ) {
		// Get the name for each coupon post
		$coupon_name    = $coupon->post_title;
		$coupon_names[] = $coupon_name;
	// Loop through the defined coupon names, and set them up if they don't already exist
	foreach ( $coupon_codes_lower as $coupon_code ) {

		// if the coupon name already exists, skip it.
		if ( in_array( $coupon_code, $coupon_names, true ) ) {

		// Get an emty instance of the WC_Coupon Object
		$coupon = new WC_Coupon();

		// Set the necessary coupon data

		// this is our coupon name
		$coupon->set_code( $coupon_code );
		// this is the discount type
		$coupon->set_discount_type( $discount_type );
		// this is the discount amount
		$coupon->set_amount( $coupon_amount );
		// this coupon can only be used once by a person
		$coupon->set_usage_limit( 1 );
		// this coupon can only be used once in total
		$coupon->set_individual_use( true );
		// this coupon can only be applied if one of these products is in the cart
		$coupon->set_product_ids( array( 1792, 1778 ) );

		// Create, publish and save the coupon settings
// run the function when WordPress initializes. You can hook this wherever.
add_action( 'init', 'aurooba_makes_bulk_add_woo_coupons' );

Get helpful tutorials like this on WordPress development every week in your inbox.

No spam, I promise.