agency-devaction/laravel-promocodes

:package_description

v0.0.1 2023-08-16 01:55 UTC

This package is auto-updated.

Last update: 2024-09-16 12:46:01 UTC


README

#StandWithUkraine laravel-promocodes

laravel-promocodes

Packagist Packagist license

Laravel 优惠券和促销码生成器。当前版本仅适用于 Laravel 9.x 和 PHP 8.1。它已完全重写,如果你在使用旧版本,应相应地更改你的代码。代码现在更简洁,你应能在几分钟内完成全部重写。

注意:当前版本已完全重写。如果你在旧版本中可以实现的功能现在缺失了,请随时提交问题。希望这个新版本会更容易使用,并为你的需求提供更好的功能。

安装

你可以通过 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)。有关更多信息,请参阅 许可证文件