a4anthony / coupon
电子商务优惠券生成器和仪表板
Requires
- ext-json: *
- doctrine/dbal: ^2.5
- illuminate/support: ~6.0|~7.0
- larapack/doctrine-support: ~0.1.4
- laravel/ui: >=1.0
Requires (Dev)
- laravel/framework: ~6.0|~7.0
- phpunit/phpcov: >=6.0
- phpunit/phpunit: >=8.0
This package is auto-updated.
Last update: 2024-09-28 09:27:31 UTC
README
安装步骤
1. 需求包
创建您的新Laravel应用程序后,可以使用以下命令包含优惠券包
composer require a4anthony/coupon:1.0.0
2. 添加数据库凭证和APP_URL
接下来,请确保创建一个新的数据库,并将您的数据库凭证添加到.env文件中
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
您还需要更新.env文件中APP_URL
变量内的网站URL
APP_URL=https://:8000
3. 安装优惠券生成器
最后,我们可以安装优惠券生成器。您可以选择带或不带示例数据安装。示例数据将包括1个管理员账户(如果没有其他用户)和5张测试优惠券
如果要无示例数据安装,请运行以下命令
php artisan coupon:install
如果您选择带示例数据安装,请运行以下命令
php artisan coupon:install --with-dummy
如果您在安装过程中遇到任何问题,请运行以下代码来自动加载所有类并重试安装
composer dump-autoload
然后我们就可以继续了!
使用 php artisan serve
启动本地开发服务器。然后访问 https://:8000/coupon/dashboard。
创建管理员用户
如果您使用了示例数据,应该已经为您创建了一个具有以下登录凭证的用户
邮箱:
admin@admin.com
密码:password
如果您没有安装示例数据并且想要创建一个新的管理员用户,可以通过传递--create
标志,如下所示
php artisan coupon:admin your@email.com --create
然后您将被提示输入用户的名称和密码。
4. 为优惠券生成器注册自定义守卫
转到config文件夹并打开auth.php文件。在providers数组中,包括以下代码
'admin' => [ 'driver' => 'eloquent', 'model' => \A4anthony\Coupon\Models\Admin::class, ]
在同一个文件中,在guards数组中,包括以下代码
'admin' => [ 'redirectTo' => 'coupon.dashboard', 'driver' => 'session', 'provider' => 'admin', ],
5. 调整RedirectIfAuthenticated类以识别自定义守卫
转到app/Http/Middleware/RedirectIfAuthenticated.php并编辑handle()方法到以下代码
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { if ($guard == "admin") { return redirect()->route('coupon.dashboard'); } return redirect(RouteServiceProvider::HOME); } return $next($request); }
6. 注册自定义中间件类
在app/Http/Middleware/RedirectIfAuthenticated.php中,在$routeMiddleware数组中包括以下代码
'admin' => \App\Http\Middleware\CouponAuthenticate::class
配置包
可以在config/coupon.php文件中配置此包。您可以配置时区,更改货币并包含货币符号,还可以设置错误消息。
/* |-------------------------------------------------------------------------- | Timezone |-------------------------------------------------------------------------- | | Here you can specify the coupon genrator timezone | */ 'timezone' => 'Africa/Lagos', /* |-------------------------------------------------------------------------- | Currency |-------------------------------------------------------------------------- | | Here you can specify the coupon genrator currency | */ 'currency' => 'NGN', /* |-------------------------------------------------------------------------- | Currency Codes |-------------------------------------------------------------------------- | | Here you can specify the coupon genrator currency codes | */ 'currency_code' => [ 'NGN' => '₦', 'USD' => '$', 'EUR' => '€', 'GBP' => '£', ], /* |-------------------------------------------------------------------------- | Error Messages |-------------------------------------------------------------------------- | | Here you can specify the coupon genrator error messages | */ 'error_msgs' => [ 'not_exist' => 'coupon does not exist', 'not_valid' => 'coupon is not valid', 'used' => 'coupon is already used by you', 'exceeds' => 'coupon discount value is greater than amount', ]
用法
使用以下命令包含包
use A4anthony\Coupon\Facades\Coupon;
1. 验证优惠券并获取折扣值
$customer_email = 'johndoe@gmail.com'; $amount = 50000; $discount = 0;//set discount default value $coupon_code = 'ANTHONYQXGA4'; try { Coupon::validate($coupon_code, $customer_email); $discount = Coupon::getDiscount($coupon_code, $amount); echo 'Coupon accepted'; } catch (Exception $e) { echo 'Message: ' . $e->getMessage(); }
2. 在成功支付时回调
Coupon::callback($coupon_code, $customer_email);
享受吧!!
贡献
欢迎Pull requests。对于重大更改,请首先打开一个问题以讨论您想要更改的内容。
请确保根据需要更新测试。