全部
常见问题
产品动态
精选推荐
功能建议

分析中 已回复 待规划 {{opt.name}}
分析中 已回复 待规划
修复门店品类券使用问题

管理 管理 编辑 删除

问题说明:门店在有自建分类权限情况下品类券关联分类不同

修改文件:app/services/activity/coupon/StoreCouponIssueServices.php

修改方法:getCanUseCoupon()

public function getCanUseCoupon(int $uid, array $cartList = [], array $promotions = [], int $store_id = 0, bool $isMax = true)
    {
        $where = ['receive_type' => 1, 'category' => 1];
        //传入是付费会员 获取付费会员券
        $isVip = $this->getItem('isVip', 0);
        if ($uid && !$isVip) {
            /** @var UserServices $userService */
            $userService = app()->make(UserServices::class);
            $isVip = $userService->checkUserIsSvip($uid);
        }
        if ($isVip) {
            unset($where['category']);
        }
        /** @var StoreCouponUserServices $coupServices */
        $coupServices = app()->make(StoreCouponUserServices::class);
        //用户持有有效券
        $userCoupons = $coupServices->getUserAllCoupon($uid);
        if ($userCoupons) {
            $where['not_id'] = array_column($userCoupons, 'cid');
        }
        $where['relation_id'] = [0, $store_id];
        $counpons = $this->dao->getValidList($where, '*', 0, 0, ['used' => function ($query) use ($uid) {
            $query->where(['uid' => $uid]);
        }]);
        //用户已经领取的 && 没有领取的
        $counpons = array_merge($counpons, $userCoupons);
        $result = [];
        if ($counpons) {
            $promotionsList = [];
            if ($promotions) {
                $promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
            }
            //验证是否适用门店
            $isApplicableStore = function ($couponInfo) use ($store_id) {
                if (isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])  && isset($couponInfo['coupon_issue_type']) && isset($couponInfo['relation_id'])) {
                    $applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
                    if (!$store_id && in_array($couponInfo['coupon_issue_type'],[1,2]) || $store_id && ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id)) || ($couponInfo['coupon_issue_type'] == 1 && $couponInfo['relation_id'] != $store_id))) {
                        return false;
                    }
                }
                return true;
            };
            //
            $isOverlay = function ($cart) use ($promotionsList) {
                $productInfo = $cart['productInfo'] ?? [];
                if (!$productInfo) {
                    return false;
                }
                //门店独立商品 不使用优惠券
//                $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
//                if ($isBranchProduct) {
//                    return false;
//                }
                if (isset($cart['promotions_id']) && $cart['promotions_id']) {
                    foreach ($cart['promotions_id'] as $key => $promotions_id) {
                        $promotions = $promotionsList[$promotions_id] ?? [];
                        if ($promotions && $promotions['promotions_type'] != 4) {
                            $overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
                            if (!in_array(5, $overlay)) {
                                return false;
                            }
                        }
                    }
                }
                return true;
            };
            /** @var StoreProductCategoryServices $storeCategoryServices */
            $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
            /** @var StoreBrandServices $storeBrandServices */
            $storeBrandServices = app()->make(StoreBrandServices::class);
            foreach ($counpons as $coupon) {
                if (isset($coupon['used'])) {
					$isValid = false;
					foreach ($coupon['used'] as $item) {//是否存在有效的
						if ($item['start_time']<= time() && $item['end_time'] >= time()) {
							$isValid = true;
							continue;
						}
					}
					if (!$isValid) {
						if (count($coupon['used']) >= $coupon['quantity_count'] && !$coupon['is_claimed']) {//不能再次领取了
							continue;
						} else {//领取的是过期无效,重新领取
							$coupon['used'] = [];
						}
					}
                }
                if (!$isApplicableStore($coupon)) {//不适用门店跳过
                    continue;
                }
                $price = 0;
                $count = 0;
                if (isset($coupon['coupon_applicable_type'])) {//已经领取 有效优惠券
                    $coupon['type'] = $coupon['coupon_applicable_type'];
                    $coupon['used'][] = ['id' => $coupon['id'], 'cid' => $coupon['cid']];
                }
                //&& in_array($promotions['promotions_type'], $overlay)
                switch ($coupon['type']) {
                    case 0:
                        foreach ($cartList as $cart) {
                            if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
                            $price = bcadd((string)$price, (string)$cart['pay_price'], 2);
                            $count++;
                        }
                        break;
                    case 1://品类券
                        $cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
                        if ($cateGorys) {
                            $cateIds = array_column($cateGorys, 'id');
                            foreach ($cartList as $cart) {
                                if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
                                if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds) || isset($cart['productInfo']['store_cate_id']) && array_intersect(explode(',', $cart['productInfo']['store_cate_id']), $cateIds)) {
                                    $price = bcadd((string)$price, (string)$cart['pay_price'], 2);
                                    $count++;
                                }
                            }
                        }
                        break;
                    case 2://商品券
                        foreach ($cartList as $cart) {
                            if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
                            $product_id = isset($cart['productInfo']['id']) && $cart['productInfo']['id'] ? $cart['productInfo']['id'] : ($cart['product_id'] ?? 0);
                            if ($product_id && in_array($product_id, explode(',', $coupon['product_id']))) {
                                $price = bcadd((string)$price, (string)$cart['pay_price'], 2);
                                $count++;
                            }
                        }
                        break;
                    case 3://品牌券
                        $brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
                        if ($brands) {
                            $brandIds = array_column($brands, 'id');
                            foreach ($cartList as $cart) {
                                if (!$isOverlay($cart) || (isset($cart['cart_type']) && $cart['cart_type'] > 0)) continue;
                                if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
                                    $price = bcadd((string)$price, (string)$cart['pay_price'], 2);
                                    $count++;
                                }
                            }
                        }
                        break;
                }
                if ($count && $coupon['use_min_price'] <= $price) {
                    //满减券
                    if ($coupon['coupon_type'] == 1) {
                        $couponPrice = $coupon['coupon_price'] > $price ? $price : $coupon['coupon_price'];
                    } else {
                        if ($coupon['coupon_price'] <= 0) {//0折
                            $couponPrice = $price;
                        } else if ($coupon['coupon_price'] >= 100) {
                            $couponPrice = 0;
                        } else {
                            $truePrice = (float)bcmul((string)$price, bcdiv((string)$coupon['coupon_price'], '100', 2), 2);
                            $couponPrice = (float)bcsub((string)$price, (string)$truePrice, 2);
                        }
                        if ($coupon['top_discount_price'] > 0 && $couponPrice > $coupon['top_discount_price']) {
                            $couponPrice = $coupon['top_discount_price'];
                        }
                    }
                    $coupon['coupon_price'] = floatval($coupon['coupon_price']);
                    $coupon['use_min_price'] = floatval($coupon['use_min_price']);
                    $coupon['true_coupon_price'] = $couponPrice;
                    $result[] = $coupon;
                }
            }
        }
        if ($result) {
            $trueCouponPrice = array_column($result, 'true_coupon_price');
            array_multisort($trueCouponPrice, SORT_DESC, $result);
        }
        //最优的一个
        if ($isMax) {
            $useCoupon = $result[0] ?? [];
            return $useCoupon;
        }
        return $result;
    }



{{voteData.voteSum}} 人已参与
支持
反对
请登录后查看

全 最后编辑于2025-12-23 10:15:18

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}} 【已收集】
{{item.floor}}# 沙发 板凳 地板 {{item.floor}}# 【已收集】
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打赏
已打赏¥{{item.reward_price}}
{{item.like_count}}
分享
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打赏
已打赏¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
收起 展开更多
查看更多
打赏
已打赏¥{{reward_price}}
11
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定
打赏金额
当前余额:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
请输入 0.1-{{reward_max_price}} 范围内的数值
打赏成功
¥{{price}}
完成 确认打赏

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服