zgabievi / laravel-promocodes
:package_description
Requires
- php: ^8.1
- illuminate/broadcasting: ^9.0
- illuminate/console: ^9.0
- illuminate/container: ^9.0
- illuminate/database: ^9.0
- illuminate/queue: ^9.0
- illuminate/support: ^9.0
Requires (Dev)
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^7.1
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.2
- pestphp/pest-plugin-mock: ^1.0
- phpunit/phpunit: ^9.0
- dev-release/9.0
- dev-main
- 9.1.0
- 9.0.0
- 8.1.2
- 8.1.1
- 8.1.0
- 8.0.0
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.5.4
- 0.5.1
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-dependabot/composer/symfony/http-kernel-6.2.6
- dev-89-validation-promo-code
- dev-bugfix/88-typehint-causing-issues-with-custom-user-model
- dev-release/8.0
- dev-release/5.1
This package is auto-updated.
Last update: 2024-09-19 23:10:16 UTC
README
laravel-promocodes
Laravel 的优惠券和促销代码生成器。[Laravel](https://laravel.net.cn) 的当前版本仅适用于 [Laravel 9.x](https://laravel.net.cn/docs/9.x) 和 [PHP 8.1](https://php.ac.cn/releases/8.1/en.php)。它已完全重写,如果您使用的是旧版本,则应相应地更改您的代码。代码现在更简化,您应该只需几分钟即可完成重写。
注意:当前版本已完全重写。如果您在旧版本中可以实现的一些功能现在缺失,请随时创建问题。希望这个新版本更容易使用,并能够满足您的需求。
安装
您可以通过 composer 安装此包
composer require zgabievi/laravel-promocodes
配置
php artisan vendor:publish --provider="Zorb\Promocodes\PromocodesServiceProvider"
现在您可以按需更改配置
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', ];
配置此文件后,运行迁移
php artisan migrate
现在您需要在用户模型上使用 AppliesPromocode
namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Zorb\Promocodes\Traits\AppliesPromocode; class User extends Authenticatable { use AppliesPromocode; // }
使用方法
使用非常简单。方法已合并,因此您可以轻松配置优惠券。
参考
创建优惠券
使用类
根据需要组合方法。您可以跳过不需要的任何方法,大多数方法都有默认值。
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();
使用助手函数
有一个全局助手函数,它可以执行与优惠券类相同的功能。您可以使用 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: [] );
使用命令
还有一个用于创建优惠券的命令。这里的参数也是可选的。
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"
生成优惠券
如果您想输出优惠券而不将其保存到数据库中,您可以调用 generate 方法而不是 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();
应用优惠券
使用类
根据需要组合方法。您可以跳过不需要的任何方法。
use Zorb\Promocodes\Facades\Promocodes; Promocodes::code('ABC-DEF') ->user(User::find(1)) // default: null ->apply();
使用助手函数
有一个全局助手函数,它可以执行与优惠券类相同的功能。
applyPomocode( 'ABC-DEF', User::find(1) // default: null );
使用命令
还有一个用于应用优惠券的命令。
php artisan promocodes:apply ABC-DEF --user=1
异常
在尝试应用优惠券时,您应该注意异常。大部分代码在出现问题时会抛出异常。
// 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."
事件
在应用时将触发两个事件。
// 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
使优惠券过期
使用助手函数
有一个全局助手函数可以过期优惠券。
expirePromocode('ABC-DEF');
使用命令
还有一个用于使优惠券过期的命令。
php artisan promocodes:expire ABC-DEF
特质方法
如果您已将 AppliesPromocode 特质添加到用户模型中,您将在用户上获得一些额外的方法。
$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
其他方法
Promocodes::all(); // To retrieve all (available/not available) promocodes Promocodes::available(); // To retrieve valid (available) promocodes Promocodes::notAvailable(); // To retrieve invalid (not available) promocodes
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。