super-pharm/laravel-promocodes2

v1.1.0 2024-03-04 16:17 UTC

This package is auto-updated.

Last update: 2024-09-04 17:23:28 UTC


README

Laravel 的优惠券和促销码生成器。当前版本仅适用于 Laravel 10.x 和 PHP 8.1。它已完全重写,如果你正在使用旧版本,你应该相应地更改你的代码。代码现在更加简化,重写使用应该只需要几分钟。

注意:当前版本已完全重写。如果你缺少在旧版本中可以实现的某些功能,请随意打开问题。希望这个新版本使用起来更简单,并能为你的需求提供更好的功能。

安装

您可以通过 composer 安装此包

composer require super-pharm/laravel-promocodes2

配置

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',
            'issued_for_id' => 'issued_for_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
          ->issuedFor(User::find(2)) // 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)。请参阅 许可证文件 了解更多信息。