Skip to content

PHP package to help generates simple and unique coupon codes

License

Notifications You must be signed in to change notification settings

iwouldrathercode/php-simple-coupons

Repository files navigation

iwouldrathercode/php-simple-coupons

Latest Version on Packagist Total Downloads

PHP package to help generates simple and unique coupon codes.

Installation

composer require iwouldrathercode/php-simple-coupons

Usage

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

Credits

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

About

PHP package to help generates simple and unique coupon codes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages