mohammadmehrabani/conditional-coupon
Composer 安装命令:
composer require mohammadmehrabani/conditional-coupon
包简介
The conditional coupon for the Laravel.
关键字:
README 文档
README
With this package, you can add both simple and conditional discount coupon features to your application. You can develop your own custom conditions, which will be automatically checked when a discount coupon is applied. If a condition is violated, an error will be returned and the coupon will not be allowed to be used by the user.
Installation
You can install the package via composer:
composer require mohammadmehrabani/conditional-coupon
You can publish and run the migrations with:
php artisan vendor:publish --tag="conditional-coupon-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="conditional-coupon-config"
This is the contents of the published config file:
return [ 'conditions' => [ // Add custom check condition classes Implemented \MohammadMehrabani\ConditionalCoupon\CheckConditionAbstract // for example: // \App\CheckUsageLimitCondition::class => 'Usage limit', // \App\CheckPaymentTypesCondition::class => 'Payment type restriction', // \App\CheckPaymentMethodsCondition::class => 'Payment method restriction', ] ];
If you want any conditions to be checked when applying a discount coupon, simply create a class that implements \MohammadMehrabani\ConditionalCoupon\CheckConditionAbstract and add it to the conditional-coupon.php config file along with a title for the condition. No further action is needed—it's that simple.
namespace App; use App\Models\Order; use MohammadMehrabani\ConditionalCoupon\CheckConditionAbstract; use MohammadMehrabani\ConditionalCoupon\Exceptions\CouponException; class CheckUsageLimitCondition extends CheckConditionAbstract { /** * @throws CouponException|\Exception */ public function handle(mixed $payload, \Closure $next): mixed { $orderCount = Order::where([ // only for example 'coupon_id' => $this->coupon->id, 'user_id' => auth()->id(), 'status' => 'fulfilled', ])->count(); if ($orderCount > $this->condition->data) { throw new \Exception(__('Coupon user usage limit reached.'), 400); } return $next($payload); } } class CheckPaymentTypesCondition extends CheckConditionAbstract { public function handle(mixed $payload, \Closure $next): mixed { if (empty(array_intersect($this->condition->data, ['cash', 'credit']))) { throw new \Exception('payment types do not match.'); } return $next($payload); } }
add to config file:
return [ 'conditions' => [ \App\CheckUsageLimitCondition::class => 'Usage limit', \App\CheckPaymentTypesCondition::class => 'Payment type restriction', ] ];
Usage
Implementing the CRUD for discount coupons—and adding conditions if needed—is up to you. Once created, you can simply use the discount coupon feature for your orders as shown in the example below.
Listing Available Check Classes
You can retrieve all available Check classes using the GetCustomConditions class.
Each Check class has a title property which is wrapped in __() for translation.
This list can be used to populate the select box when creating discount coupons.
use MohammadMehrabani\ConditionalCoupon\GetCustomConditions; GetCustomConditions::handle();
create coupon with conditions
use MohammadMehrabani\ConditionalCoupon\Models\Coupon; use MohammadMehrabani\ConditionalCoupon\Models\CouponCondition; $coupon = Coupon::create([ 'code' => 'AMZ100', // fill other columns ]); $couponCondition = CouponCondition::create([ 'coupon_id' => $coupon->id, 'condition' => \App\CheckUsageLimitCondition::class, 'data' => 1 ]); $couponCondition = CouponCondition::create([ 'coupon_id' => $coupon->id, 'condition' => \App\CheckPaymentTypesCondition::class, 'data' => ["credit", "cash"], ]);
create coupon without conditions
use MohammadMehrabani\ConditionalCoupon\Models\Coupon; use MohammadMehrabani\ConditionalCoupon\Models\CouponCondition; $coupon = Coupon::create([ 'code' => 'AMZ100', // fill other columns ]);
use coupon for order
use Illuminate\Support\Facades\DB; use App\Models\Order; use MohammadMehrabani\ConditionalCoupon\InquiryCoupon; DB::transaction(function () { $inquiryCoupon = new InquiryCoupon(); [ $currency, $amount, $discountAmount, $payableAmount, $coupon ] = $inquiryCoupon->handle(code: request()->get('coupon_code'), amount: 1000000, locked: true); $order = Order::create([ // only for example 'discount' => $discountAmount, 'amount' => $payableAmount, 'user_id' => auth()->id(), 'status' => 'initial', 'coupon_id' => $coupon->id, # Other fields as needed: // 'coupon_code' => $coupon->code, // 'discount_percentage' => $coupon->discount_percentage, // 'discount_amount' => $coupon->discount_amount, ]); $coupon->increment('used_count'); });
Discount Logic
The package supports two types of discount fields:
discount_percentagediscount_amount
Rules
-
Single Field Case
- If only one of the fields is provided, the discount will be applied based on that field.
-
Both Fields Case
- If both
discount_percentageanddiscount_amountare provided:- The discount value is first calculated from the
discount_percentage. - If the calculated percentage discount is greater than the fixed
discount_amount, then the fixed amount will be applied. - If the calculated percentage discount is less than or equal to the fixed
discount_amount, then the percentage-based discount will be applied.
- The discount value is first calculated from the
- If both
Example
- Order total: $200
discount_percentage: 20%discount_amount: $30
Calculation:
- 20% of $200 = $40
- Since $40 (percentage) > $30 (amount), the final discount will be $30.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
mohammadmehrabani/conditional-coupon 适用场景与选型建议
mohammadmehrabani/conditional-coupon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「discount」 「coupon」 「promo」 「promo code」 「coupon code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mohammadmehrabani/conditional-coupon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohammadmehrabani/conditional-coupon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mohammadmehrabani/conditional-coupon 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds Discount and Coupon support for SilverShop.
Coupon Error Message extension
Generate Apple Passbooks from your Symfony application
Magento 2 Discount Amount Limiter
Adds Discount and Coupon support for SilverShop.
Extension of Magento2 Sales Rule to allow multiple coupon usage.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-02