在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):zgabievi/laravel-promocodes开源软件地址(OpenSource Url):https://github.com/zgabievi/laravel-promocodes开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):laravel-promocodesCoupons and promotional codes generator for Laravel. Current release is only for Laravel 9.x and PHP 8.1. It's completely rewritten, and if you are using previous version, you should change your code accordingly. Code is simplified now and it should take you several minutes to completely rewrite usage.
If you want to use this package with Laravel Nova, please install zgabievi/nova-promocodes. InstallationYou can install the package via composer: composer require zgabievi/laravel-promocodes Configurationphp artisan vendor:publish --provider="Zorb\Promocodes\PromocodesServiceProvider" Now you can change configurations as you need: return [
'models' => [
'promocodes' => [
'model' => \Zorb\Promocodes\Models\Promocode::class,
'table_name' => 'promocodes',
'foreign_id' => 'promocode_id',
],
'users' => [
'model' => \App\Models\User::class,
'table_name' => 'users',
'foreign_id' => 'user_id',
],
'pivot' => [
'model' => \Zorb\Promocodes\Models\PromocodeUser::class,
'table_name' => 'promocode_user',
],
],
'code_mask' => '****-****',
'allowed_symbols' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789',
]; 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;
//
} UsageIt's very easy to use. Methods are combined, so that you can configure promocodes easily. Reference
Creating PromocodesUsing classCombine 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 helperThere is a global helper function which 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 commandThere 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 PromocodesIf you want to output promocodes and not save them to 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 PromocodeUsing classCombine 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 helperThere is a global helper function which will do the same as promocodes class. applyPomocode(
'ABC-DEF',
User::find(1) // default: null
); Using commandThere is also the command for applying promocode. php artisan promocodes:apply ABC-DEF --user=1 ExceptionsWhile trying to apply promocode, you should be aware of exceptions. Most part of the code throws exceptions, when there is a problem: // Zorb\Promocodes\Exceptions\*
PromocodeAlreadyUsedByUserException - "The given code `ABC-DEF` is already used by user with id 1."
PromocodeBoundToOtherUserException - "The given code `ABC-DEF` is bound to other user, not user with id 1."
PromocodeDoesNotExistException - "The given code `ABC-DEF` doesn't exist." | "The code was not event provided."
PromocodeExpiredException - "The given code `ABC-DEF` already expired."
PromocodeNoUsagesLeftException - "The given code `ABC-DEF` has no usages left."
UserHasNoAppliesPromocodeTrait - "The given user model doesn't have AppliesPromocode trait."
UserRequiredToAcceptPromocode - "The given code `ABC-DEF` requires to be used by user, not by guest." EventsThere are two events which are fired upon applying. // Zorb\Promocodes\Events\*
GuestAppliedPromocode // Fired when guest applies promocode
// It has public variable: promocode
UserAppliedPromocode // Fired when user applies promocode
// It has public variable: promocode
// It has public variable: user Expiring PromocodeUsing helperThere is a global helper function which will expire promocode. expirePromocode('ABC-DEF'); Using commandThere is also the command for expiring promocode. php artisan promocodes:expire ABC-DEF Trait MethodsIf you added AppliesPromocode trait to your user model, you will have some additional methods on user. $user = User::find(1);
$user->appliedPromocodes; // Returns promocodes applied by user
$user->boundPromocodes; // Returns promocodes bound to user
$user->applyPromocode('ABC-DEF'); // Applies promocode to user Testingcomposer test ContributingPlease see CONTRIBUTING for details. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论