Laravel-promocodes is an awesome coupon and promotional code generator for Laravel. The current release is for Laravel 9.x
and PHP 8.1. It's completely rewritten, if you use the previous version, you should change your code accordingly. The code is simplified now and it should take you several minutes to completely rewrite usage.
Installation
You can install the package via composer:
composer require zgabievi/laravel-promocodes
Configuration
php artisan vendor:publish --provider="Zorb\Promocodes\PromocodesServiceProvider"
After you configure this file, run migrations:
php artisan migrate
Now you will need to use AppliesPromocode on your user model.
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zorb\Promocodes\Traits\AppliesPromocode;
class User extends Authenticatable {
use AppliesPromocode;
//
}
Usage
It's very easy to use. Methods are combined so that you can configure promo codes easily.
Creating Promo codes
Using class
Combine methods as you need. You can skip any method that you don't need, most of them already have default values.
use Zorb\Promocodes\Facades\Promocodes;
Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
->multiUse() // default: false
->unlimited() // default: false
->boundToUser() // default: false
->user(User::find(1)) // default: null
->count(5) // default: 1
->usages(5) // default: 1
->expiration(now()->addYear()) // default: null
->details([ 'discount' => 50 ]) // default: []
->create();
Using helper
There is a global helper function that will do the same as promocodes class. You can use named arguments magic from php 8.1.
createPromocodes(
mask: 'AA-***-BB', // default: config('promocodes.code_mask')
characters: 'ABCDE12345', // default: config('promocodes.allowed_symbols')
multiUse: true, // default: false
unlimited: true, // default: false
boundToUser: true, // default: false
user: User::find(1), // default: null
count: 5, // default: 1
usages: 5, // default: 1
expiration: now()->addYear(), // default: null
details: [ 'discount' => 50 ] // default: []
);
Using command
There is also the command for creating promocodes. Parameters are optional here too.
php artisan promocodes:create\
--mask="AA-***-BB"\
--characters="ABCDE12345"\
--multi-use\
--unlimited\
--bound-to-user\
--user=1\
--count=5\
--usages=5\
--expiration="2022-01-01 00:00:00"
Generating Promocodes
If you want to output promocodes and not save them to the database, you can call generate method instead of create.
use Zorb\Promocodes\Facades\Promocodes;
Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
->multiUse() // default: false
->unlimited() // default: false
->boundToUser() // default: false
->user(User::find(1)) // default: null
->count(5) // default: 1
->usages(5) // default: 1
->expiration(now()->addYear()) // default: null
->details([ 'discount' => 50 ]) // default: []
->generate();
Applying Promocode
Using class
Combine methods as you need. You can skip any method that you don't need.
use Zorb\Promocodes\Facades\Promocodes;
Promocodes::code('ABC-DEF')
->user(User::find(1)) // default: null
->apply();
Using helper
There is a global helper function that will do the same as the promocodes class.
applyPomocode(
'ABC-DEF',
User::find(1) // default: null
);
Using command
There is also the command for applying promocode.
php artisan promocodes:apply ABC-DEF --user=1
For more details and source code, please visit Github.
0 comments:
Post a Comment
Thanks