Skip to content

Instantly share code, notes, and snippets.

@gabrielboliveira
Created June 23, 2022 01:25
Show Gist options
  • Save gabrielboliveira/431914f5ca6c162a05a7ab9e38549599 to your computer and use it in GitHub Desktop.
Save gabrielboliveira/431914f5ca6c162a05a7ab9e38549599 to your computer and use it in GitHub Desktop.
Generate random Coupon Codes for Easy Digital Downloads
<?php
$amountOfCoupons = 10;
$minLength = 8;
$maxLength = 14;
// From StackOverflow https://stackoverflow.com/a/13212994
function generateRandomString( $length = 10 ) {
$x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr( str_shuffle( str_repeat( $x, ceil( $length / strlen( $x ) ) ) ), 1, $length );
}
function generateRandomCouponCodes( $amount = 10, $minLength = 8, $maxLength = 14 ) {
for ($i = 1; $i <= $amount; $i++) {
edd_store_discount([
'code' => generateRandomString( rand( $minLength, $maxLength ) ),
'name' => "Coupon Test {$i}",
'amount' => rand( 3, 100 )
]);
}
}
generateRandomCouponCodes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment