Home » WooCommerce: How to Bulk Generate Coupons Without a Plugin

WooCommerce: How to Bulk Generate Coupons Without a Plugin

by Tutor Aspire

There are times when doing manual work – such as creating a WooCommerce coupon – is too time consuming. Imagine you need to bulk generate 1,000 coupon codes – sure you have 2,000 minutes at your disposal to do it all by hand?

Well, today, we’ll take a look at how to bulk generate coupon codes from the WordPress backend by using a simple PHP “for“. Once the function triggers, it will just be a matter of seconds!

Enjoy!

Here’s how I bulk created 100 coupons in a few seconds with a simple snippet. If you need to generate thousands of them, make sure you don’t exceed the maximum WordPress load time – in this case I recommend running the function N times as opposed to using a $number_of_coupons greater than 1,000.

PHP Snippet: Bulk Create Coupon Codes @ WordPress Dashboard

Note: in order to trigger the function, you must go to any WordPress admin URL with the “bb-gen-coupons” URL parameter e.g. /wp-admin/admin.php?bb-gen-coupons. This will make sure the coupons are generated.

/**
 * @snippet       Bulk Generate Coupons @ WordPress Admin
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'admin_init', 'tutoraspire_generate_coupons_admin' );

function tutoraspire_generate_coupons_admin() {
if ( isset( $_REQUEST['bb-gen-coupons'] ) ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( 'You do not have permission to bulk generate coupons' );
}
$number_of_coupons = 100; // DEFINE BULK QUANTITY
for ( $i = 1; $i set_code( $random_code );
$coupon->set_description( 'Coupon generated programmatically (' . $i . '/' . $number_of_coupons . ')' );
$coupon->set_discount_type( 'percent' );
$coupon->set_amount( 20 );
$coupon->set_minimum_amount( 1 );
$coupon->set_individual_use( true );
$coupon->set_product_categories( array( 54, 55 ) );
$coupon->set_usage_limit_per_user( 1 );
$coupon->save();
}
}
}

You may also like