优惠券次数限制bug

bug问题 · helloworld · 于 5年前 发布 · 2166 次阅读

fecshop/services/cart/Coupon.php第285行左右, 如果设置每用户只能使用1次,使用优惠券时会添加一条优惠券使用记录并把次数设置为1,使用成功后会刷新购物车页面,又再判断一次 次数限制,1 <1,就无法使用优惠券了,应该改为1 <=1.

    /**
     * 查看coupon是否是可用的,如果可用,返回true,如果不可用,返回false
     */
    protected function couponIsActive()
    {
        if ($this->_customer_id) {
            if ($couponModel = $this->getCouponModel()) {
                $expiration_date = $couponModel['expiration_date'];
                Yii::info('$expiration_date:'.$expiration_date,'fecshop_debug');
                // 未过期
                if ($expiration_date > time()) {
                    $couponUsageModel = $this->getCouponUsageModel();
                    $times_used = 0;
                    if ($couponUsageModel['times_used']) {
                        $times_used = $couponUsageModel['times_used'];
                    }
                    $users_per_customer = $couponModel['users_per_customer'];
                    Yii::info('$times_used:'.$times_used,'fecshop_debug');
                    // 次数限制
                    if ($times_used <= $users_per_customer) {
                        Yii::info('yes:!','fecshop_debug');
                        return true;
                    } else {
                        Yii::$service->helper->errors->add('The coupon has exceeded the maximum number of uses');
                    }
                } else {
                    Yii::$service->helper->errors->add('coupon is expired');
                }
            } else {
                //Yii::$service->helper->errors->add("coupon is not exist");
            }
        }
        return false;
    }
共收到 4 条回复 fecshop
Fecmall#15年前 0 个赞

明天核验下,fecshop的优惠券实现的比较简单

Fecmall#25年前 0 个赞

下面的代码是没有问题的

// 次数限制
                    if ($times_used <= $users_per_customer) {
                        Yii::info('yes:!','fecshop_debug');
                        return true;
                    } else {
                        Yii::$service->helper->errors->add('The coupon has exceeded the maximum number of uses');
                    }

当你设置了每个用户最多使用一次,也就是$users_per_customer为1,那么你第一次使用的时候,这个优惠券是没有被你使用的,因此 $times_used的值是0,因此条件满足,是可以使用的。

因此,代码是没有问题的

helloworld#35年前 0 个赞

@Fecshop #2楼 第一次 addCoupon是 $cu_model->times_used = 1;为1的

/**
     * @property $type | String     add or cancel
     * @return bool
     *              增加或者减少优惠券使用的次数
     */
    protected function updateCouponUse($type)
    {
        if ($this->_customer_id) {
            $c_model = $this->getCouponModel();
            if ($c_model) {
                if ($type == 'add') {
                    $cu_model = $this->getCouponUsageModel();
                    if (!$cu_model) {
                        $cu_model = new $this->_couponUsageModelName;
                        $cu_model->times_used = 1;
                        $cu_model->customer_id = $this->_customer_id;
                        $cu_model->coupon_id = $c_model['coupon_id'];
                        $cu_model->save();
                    } else {
                        Yii::info('是否新增优惠券使用次数!1','fecshop_debug');
                        // 通过update函数 将times_used +1
                        $sql = 'update '.$this->_couponUsageModel->tableName().' set times_used = times_used + 1 where id = :id';
                        $data = [
                            'id'  => $cu_model['id'],
                        ];
                        Yii::info('sql:'.$sql,'fecshop_debug');
                        Yii::info('$cu_model[\'id\']:'.$cu_model['id'],'fecshop_debug');
                        $result = $this->_couponUsageModel->getDb()->createCommand($sql,$data)->execute();
                        Yii::info('是否新增优惠券使用次数!2','fecshop_debug');
                    }
                    // coupon的总使用次数+1
                    $sql = 'update '.$this->_couponModel->tableName().' set times_used = times_used + 1 where coupon_id  = :coupon_id ';
                    $data = [
                        'coupon_id' => $c_model['coupon_id'],
                    ];
                    $result = $this->_couponModel->getDb()->createCommand($sql,$data)->execute();
                    return true;
                } elseif ($type == 'cancel') {
                    $couponModel = $this->getCouponModel();
                    $cu_model = $this->getCouponUsageModel();
                    if ($cu_model) {
                        // $this->_couponUsageModel 使用次数-1
                        $sql = 'update '.$this->_couponUsageModel->tableName().' set times_used = times_used - 1 where id = :id';
                        $data = [
                            'id'  => $cu_model['id'],
                        ];
                        $result = $this->_couponUsageModel->getDb()->createCommand($sql,$data)->execute();
                        // $this->_couponModel 使用次数-1
                        $sql = 'update '.$this->_couponModel->tableName().' set times_used = times_used - 1 where coupon_id  = :coupon_id ';
                        $data = [
                            'coupon_id' => $c_model['coupon_id'],
                        ];
                        $result = $this->_couponModel->getDb()->createCommand($sql,$data)->execute();
                        
                        return true;
                    }
                }
            }
        }
    }
Fecmall#45年前 0 个赞

我测试了一下,你说的这个问题的确存在

couponIsActive 是判断优惠券是否有效的函数

addCoupon的时候,需要判断次数

getDiscount(获取折扣)的时候,是购物车中的优惠券,不需要判断使用次数,因此,进行了小调整

代码已经提交:https://github.com/fecshop/yii2_fecshop/commit/9e336ca292ce1e8ce5f0156b9d7399207e489218 ,你按照上面修改后测试看看是否还有问题。

多谢提交bug

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics