aberkanidev/laravel-coupons

Laravel优惠券生成器

v1.1.0 2021-01-06 12:26 UTC

This package is auto-updated.

Last update: 2024-09-06 20:37:06 UTC


README

Latest Stable Version Total Downloads License

Laravel coupons 是一个允许您将优惠券系统添加到您的 Laravel 应用程序的包。优惠券可以与 Eloquent 模型关联,并且可以通过您选择的任何 Eloquent 模型兑换,而不仅仅是用户模型。

以下是一个示例

$item = Item::find(1);
$customer = Customer::find(1);

$coupon = $Item->createDisposableCoupon();

$customer->redeemCoupon($coupon);

目录

  • 安装
  • 使用
  • 许可证

安装

使用 composer 安装此包

composer require aberkanidev/laravel-coupons

运行以下命令以发布迁移

php artisan vendor:publish --provider="aberkanidev\Coupons\CouponsServiceProvider" --tag="migrations"

运行以下命令以发布配置文件

php artisan vendor:publish --provider="aberkanidev\Coupons\CouponsServiceProvider" --tag="config"

配置文件示例

<?php

return [

    /*
     * Database table name that will be used in migration
     */
    'table' => 'coupons',

    /*
     * Database table name for voucherable models
     */
    'voucherable_table' => 'voucherables',

    /*
     * List of characters that will be used for coupon code generation.
     */
    'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',

    /*
     * Coupon code prefix.
     *
     * Example: foo
     * Generated Code: foo-AGXF-1NH8
     */
    'prefix' => null,

    /*
     * Coupon code suffix.
     *
     * Example: foo
     * Generated Code: AGXF-1NH8-foo
     */
    'suffix' => null,

    /*
     * Code mask.
     * All asterisks will be removed by random characters.
     */
    'mask' => '****-****',

    /*
     * Separator to be used between prefix, code and suffix.
     */
    'separator' => '-',
];

使用

要使用此包,您必须将此特性 aberkanidev\Coupons\Traits\HasCoupons 添加到将与优惠券关联的 Eloquent 模型。

此外,您还必须将 aberkanidev\Coupons\Traits\CanRedeemCoupons 添加到可以兑换优惠券的 Eloquent 模型(例如:用户、客户等)。

创建优惠券

代码示例

$item = Item::find(1);

// Create 10 coupons using the Facade
// Returns an array of Coupons
$coupons = Coupons::create($item, 5);

// Create 3 coupons using the Eloquent model
// Returns an array of Coupons
$coupons = $item->createCoupons(2);

// Create a single coupon
$coupons = $item->createCoupon();

创建一次性优惠券

$item = Item::find(1);

// Create a disposable coupon
$coupon = $item->createDisposableCoupon();

创建带有附加数据的优惠券

$item = Item::find(1);

$coupons = $item->createCoupons(4, [
    'amount' => '150',
    'recipient_name' => 'Mohamed Aberkani'
		'recipient_email' => 'aberkanidev@gmail.com'
]);

$coupons = $coupons[0];
$coupon->data->get('amount');
$coupon->data->get('recipient_name');
$coupon->data->get('recipient_email');

创建带有到期日期的优惠券

$item = Item::find(1);

$item->createCoupons(4, [], today()->addDays(3));

兑换优惠券代码

兑换优惠券有两种方式:使用代码或优惠券模型。

$customer = Customer::find(1);

// Redeem using the code
// Returns the Coupon model
$coupon = $customer->redeem('ABCD-EFGH');

如果代码有效,它将返回优惠券模型。

兑换优惠券模型

$customer = Customer::find(1);

$item = Item::find(1);

// Create Coupon
$coupon = $item->createCoupon();

// Redeem the Coupon Model
$coupon = $customer->redeemCoupon($coupon);

此操作(兑换优惠券)将触发 aberkanidev\Coupons\Events\CouponRedeemed 事件。

访问与优惠券模型关联的模型

$customer = Customer::find(1);
$coupon = $customer->redeemCoupon('ABCD-EFGH');

$item = $coupon->model

许可证

Laravel coupons 采用 MIT 许可证 许可。