michael-rubel/laravel-couponables
此包为您的Laravel应用程序提供多态优惠券功能。
9.0.0
2024-03-15 21:09 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.13
Requires (Dev)
- brianium/paratest: ^6.3|^7.4
- infection/infection: ^0.27.3
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
README
Laravel Couponables
此包通过利用Eloquent的多态关系为您的Laravel应用程序提供优惠券/促销码功能。
该包需要PHP 8.1或更高版本以及Laravel 10或更高版本。
#StandWithUkraine
安装
使用Composer安装包
composer require michael-rubel/laravel-couponables
发布迁移
php artisan vendor:publish --tag="couponables-migrations"
发布配置文件
php artisan vendor:publish --tag="couponables-config"
使用方法
发布迁移后,在您想用作$redeemer
的模型中应用一个特例
use HasCoupons;
Artisan命令
您可以使用Artisan命令将优惠券添加到数据库中
php artisan make:coupon YourCouponCode
可选地,您可以传递以下参数
'--value' // The 'value' to perform calculations based on the coupon provided '--type' // The 'type' to point out the calculation strategy '--limit' // Limit how many times the coupon can be applied by the model '--quantity' // Limit how many coupons are available overall (this value will decrement) '--expires_at' // Set expiration time for the coupon '--redeemer_type' // Polymorphic model type. Can as well be morph-mapped value, i.e. 'users' '--redeemer_id' // Redeemer model ID '--data' // JSON column to store any metadata you want for this particular coupon
使用模型添加优惠券
您也可以简单地使用模型添加优惠券
Coupon::create([ 'code' => '...', 'type' => '...' 'value' => '...', ... ]);
- 注意:
type
和value
列用于成本计算(这是可选的)。
如果type
列是null
,则选择subtraction
策略。
基本操作
验证优惠券代码
$redeemer->verifyCoupon($code);
兑换优惠券
$redeemer->redeemCoupon($code);
在另一个模型的上下文中兑换优惠券
$redeemer ->redeemCoupon($code) ->for($course);
组合redeemCoupon
和for
行为(假设$course
包含HasCoupons
特例)
$course->redeemBy($redeemer, $code);
如果出现错误,则verifyCoupon
和redeemCoupon
方法将抛出异常
CouponDisabledException // Coupon is disabled (`is_enabled` column). CouponExpiredException // Coupon is expired (`expires_at` column). InvalidCouponException // Coupon is not found in the database. InvalidCouponTypeException // Wrong coupon type found in the database (`type` column). InvalidCouponValueException // Wrong coupon value passed from the database (`value` column). NotAllowedToRedeemException // Coupon is assigned to the specific model (`redeemer` morphs). OverLimitException // Coupon is over the limit for the specific model (`limit` column). OverQuantityException // Coupon is exhausted (`quantity` column). CouponException // Generic exception for all cases.
如果您想绕过异常并执行其他操作
$redeemer->verifyCouponOr($code, function ($code, $exception) { // Your action with $code or $exception! });
$redeemer->redeemCouponOr($code, function ($code, $exception) { // Your action with $code or $exception! });
兑换者检查
检查此优惠券是否已被模型使用
$redeemer->isCouponAlreadyUsed($code);
检查优惠券是否超过模型的限制
$redeemer->isCouponOverLimit($code);
优惠券检查
public function isExpired(): bool; public function isNotExpired(): bool; public function isDisposable(): bool; public function isOverQuantity(): bool; public function isRedeemedBy(Model $redeemer): bool; public function isOverLimitFor(Model $redeemer): bool;
此方法引用分配给兑换优惠券的模型
public function redeemer(): ?Model;
计算
$coupon = Coupon::create([ 'code' => 'my-generated-coupon-code-to-use', 'type' => CouponContract::TYPE_PERCENTAGE, // 'percentage' 'value' => '10', // <-- %10 ]); $coupon->calc(using: 300); // 270.00
该包支持三种类型的商品成本计算
subtraction
- 从优惠券模型中定义的值中减去给定的值;percentage
- 通过优惠券模型中定义的百分比减去给定的值;fixed
- 完全忽略给定的值,而是使用优惠券模型值。
注意:您可以在CouponContract
中找到优惠券类型的常量。
监听器
如果您使用事件驱动,则可以处理包事件
- CouponVerified
- CouponRedeemed
- CouponExpired
- CouponIsOverLimit
- CouponIsOverQuantity
- NotAllowedToRedeem
扩展包功能
特例DefinesColumns和DefinesPivotColumns包含定义包使用的列名的方 法,因此您可以使用继承来覆盖它们。
如果您需要完全覆盖类,请使用配置值或容器绑定。包中的所有类都有自己的合同(接口),因此您可以根据需要修改它。
CouponService
具有带有 Macroable
特性的功能,这样您可以注入与该服务交互的方法,而无需重写任何内容。
贡献
如果您看到任何可以改进该包的方法,我们欢迎提交 Pull Request。但请记住为您的情况编写测试。
测试
composer test