PHP package to help generates simple and unique coupon codes.
composer require iwouldrathercode/php-simple-coupons
Generating single coupon codes
use Iwouldrathercode\SimpleCoupons\Coupon;
.
.
.
// Somewhere in your code...
$code = new Coupon();
// Coupon code only
$coupon = $code->generate();
// Coupon code with a prefix of `ABC`
$coupon = $code->prepend('ABC')->generate();
// Coupon code with a prefix of `ABC` and suffix of `XYZ`
$coupon = $code->prepend('ABC')->append('XYZ')->generate();
// Coupon code with a prefix of `ABC` and suffix of `XYZ` and max. char. length as - 10
$coupon = $code->limit(10)->generate();
.
.
Generating multiple coupon codes
use Iwouldrathercode\SimpleCoupons\Coupon;
.
.
.
// Somewhere in your code...
function generateMultipleCodes($limit, $table)
{
$couponsArray = [];
$code = new Coupon();
// Looping through unti limit is reached
for($i=1; $i<=$limit; $i++) {
// VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
gc_enable();
// Call to function to generate one coupon code
generate($code, $couponsArray);
// VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
gc_disable();
}
// VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
unset($code);
return $array;
}
function generate($code, $couponsArray)
{
$coupon = $code->limit(12)->generate();
// echo $coloredOutput->apply("color_15", $coupon.PHP_EOL);
array_push($couponsArray, $coupon);
$code->__destruct();
}
// Generate 10 coupon codes
$multipleCoupons = generateMultipleCodes(10, $table);
For more examples do refer example.php and performance.php
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.