How to generate a bunch of single-use discount codes in Easy Digital Downloads

I needed to create a bunch of one-time use discount codes for a store, and created a script to do just that. There is an extension on the Easy Digital Downloads store if you need a nicer interface, but the code is pretty straightforward.

1. Create the plugin file

Create a new file called wp-content/plugins/edd-generate-discounts.php and put the following code inside:

<?php
/*
Plugin Name: EDD Generate 100% Discounts
Description: Generate discounts for a cross-sale promo
Version: 1.0
Author: Brian Hogg
Licence: GPLv2
*/

function edd_mpp_generate_random_string( $length = 10 ) {
   $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $characters_length = strlen( $characters );
   $randomString = '';
   for ( $i = 0; $i < $length; $i++ ) {
      $random_string .= $characters[rand(0, $characters_length - 1)];
   }
   return $random_string;
}

function edd_mpp_generate_codes() {
   if ( isset( $_GET['generate_codes'], $_GET['generate_code_count'] ) and '90kjas09df2j09sdfkj1039wdsfj' == $_GET['generate_codes'] and is_numeric( $_GET['generate_code_count'] ) ) {
      for ( $i = 0; $i < intval( $_GET['generate_code_count'] ); $i++ ) {
         $args = array(
            'name'              => 'Deals Discount Code',
            'code'              => edd_mpp_generate_random_string( 20 ),
            'type'              => ( ( isset( $_GET['generate_code_type'] ) and 'flat' == $_GET['generate_code_type'] ) ? 'flat' : 'percentage' ),
            'amount'            => ( ( isset( $_GET['generate_code_amount'] ) and is_numeric( $_GET['generate_code_amount'] ) ) ? intval( $_GET['generate_code_amount'] ) : '100' ),
            'excluded_products' => array(),
            'expiration'        => '',
            'is_not_global'     => false,
            'is_single_use'     => true,
            'max'               => 1,
            'min_price'         => '',
            'product_condition' => '',
            'product_reqs'      => array(),
            'start'             => '',
            'uses'              => '',
         );
         edd_store_discount( $args );
         echo $args['code'] . "
"; } die(); } } add_action( 'admin_init', 'edd_mpp_generate_codes' );

Now go to Plugins and activate this plugin. You can change the parameters as needed, for example to limit to only certain products. You might also want to put a separate condition for the function to run,such as a current_user_can check.

2. Generate the codes

Login to your WordPress admin and visit https://yoursite.com/wp-admin/?generate_codes=90kjas09df2j09sdfkj1039wdsfj&generate_code_count=50 replacing 50 with however many codes you want to generate.

You can also add &generate_code_type=flat to generate a flat dollar amount discount instead of a percentage, and &generate_code_amount=50 if (say) you want to change the dollar amount or percentage to 50.

The codes will generate and output one per line on the screen.

3. Deactivate the plugin

Even though there is a key to help prevent codes from being generated randomly, safer to just disable the plugin to avoid any issues.