Created
June 23, 2022 01:25
-
-
Save gabrielboliveira/431914f5ca6c162a05a7ab9e38549599 to your computer and use it in GitHub Desktop.
Generate random Coupon Codes for Easy Digital Downloads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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