9.5. 优化优惠券模块
优化优惠券模块
上一节我们完成了使用优惠券来下单的基本功能,接下来我们要完善一些遗漏的功能。
一张优惠券一个用户只能使用一次
通常来说一张优惠券对每一个用户来说只能使用一次,这里我们对『使用』的定义是:有关联了此优惠券的未付款且未关闭订单或者已付款且未退款成功订单,根据这个规则我们来完善一下 CouponCode
模型中的 checkAvailable()
方法:
app/Models/CouponCode.php
.
.
.
// 添加了一个 $user 参数
public function checkAvailable(User $user, $orderAmount = null)
{
.
.
.
$used = Order::where('user_id', $user->id)
->where('coupon_code_id', $this->id)
->where(function($query) {
$query->where(function($query) {
$query->whereNull('paid_at')
->where('closed', false);
})->orWhere(function($query) {...