super-pharm/laravel-promocodes2
Composer 安装命令:
composer require super-pharm/laravel-promocodes2
包简介
:package_description
关键字:
README 文档
README
Coupons and promotional codes generator for Laravel. Current release is only for Laravel 10.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.
Attention: Current version is completely rewritten. If you are missing some functionality, that was possible to achieve in previous versions, fill free to open issue. Hope this new version will be easier to use, and it will provide better functionality for your needs.
Installation
You can install the package via composer:
composer require super-pharm/laravel-promocodes2
Configuration
php 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', 'issued_for_id' => 'issued_for_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; // }
Usage
It's very easy to use. Methods are combined, so that you can configure promocodes easily.
- Reference
- Creating Promocodes
- Generating Promocodes
- Applying Promocode
- Expiring Promocode
- Additional Methods
Reference
| Name | Explanation |
|---|---|
| Mask | Astrisks will be replaced with random symbol |
| Characters | Allowed symbols to use in mask replacement |
| Multi use | Define if single code can be used multiple times, by the same user |
| Unlimited | Generated code will have unlimited usages |
| Bound to user | Define if promocode can be used only one user, if user is not assigned initially, first user will be bound to promocode |
| Issued for user | Define the user that the promocode was issued for |
| User | Define user who will be initially bound to promocode |
| Count | Amount of unique promocodes should be generated |
| Usages | Define how many times can promocode be used |
| Expiration | DateTime when promocode should be expired. Null means that promocode will never expire |
| Details | Array of details which will be retrieved upon apply |
Creating Promocodes
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 ->issuedFor(User::find(2)) // 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 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 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 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 which will do the same as 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
Exceptions
While 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."
Events
There 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 Promocode
Using helper
There is a global helper function which will expire promocode.
expirePromocode('ABC-DEF');
Using command
There is also the command for expiring promocode.
php artisan promocodes:expire ABC-DEF
Trait Methods
If 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
Additional Methods
Promocodes::all(); // To retrieve all (available/not available) promocodes Promocodes::available(); // To retrieve valid (available) promocodes Promocodes::notAvailable(); // To retrieve invalid (not available) promocodes
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
super-pharm/laravel-promocodes2 适用场景与选型建议
super-pharm/laravel-promocodes2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 574 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「promocodes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 super-pharm/laravel-promocodes2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 super-pharm/laravel-promocodes2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 super-pharm/laravel-promocodes2 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
:package_description
Laravel 10+ compatible promotion codes generator and validator.
Manage ecommerce vouchers
:package_description
:package_description
zgabievi promocodes fork with laravel 10 support
统计信息
- 总下载量: 574
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-22