diff --git a/addons/shopro/controller/Goods.php b/addons/shopro/controller/Goods.php index d86e40f..10d7255 100644 --- a/addons/shopro/controller/Goods.php +++ b/addons/shopro/controller/Goods.php @@ -46,6 +46,35 @@ class Goods extends Base $detail['cartNum'] = count($cartList); } + // 复购立减提示信息 + $detail['repurchase_info'] = [ + 'is_enable' => false, + 'is_user_repurchase' => false, + 'tip' => '' + ]; + try { + $isEnable = \addons\shopro\library\commission\RepurchaseService::isGoodsRepurchaseEnable($id); + if ($isEnable) { + $config = \addons\shopro\library\commission\RepurchaseService::getConfig(); + if (!empty($config['enable']) && !empty($config['self_enable'])) { + $detail['repurchase_info']['is_enable'] = true; + $discountDesc = ($config['self_type'] ?? 'fixed') === 'rate' + ? ($config['self_rate'] . '%') + : ('¥' . $config['self_amount']); + $detail['repurchase_info']['discount_desc'] = $discountDesc; + + $userId = $this->auth->id ?? 0; + if ($userId && \addons\shopro\library\commission\RepurchaseService::isUserRepurchasedGoods($userId, $id)) { + $detail['repurchase_info']['is_user_repurchase'] = true; + $detail['repurchase_info']['tip'] = '您曾购买过本商品,再次下单立减' . $discountDesc; + } else { + $detail['repurchase_info']['tip'] = '老客复购享立减' . $discountDesc; + } + } + } + } catch (\Throwable $e) { + } + $this->success('商品详情', $detail); } diff --git a/addons/shopro/tests/repurchase_discount_test.php b/addons/shopro/tests/repurchase_discount_test.php index 0e7d1f2..aec6179 100644 --- a/addons/shopro/tests/repurchase_discount_test.php +++ b/addons/shopro/tests/repurchase_discount_test.php @@ -27,6 +27,7 @@ class RepurchaseDiscountTest $this->testRepurchaseRateDiscount(); $this->testDiscountCapProtection(); $this->testMixedOrderDiscounts(); + $this->testGoodsDetailRepurchaseTip(); echo "\n----------------------------------------\n"; echo "测试结果: {$this->passed} 通过, {$this->failed} 失败\n"; @@ -265,6 +266,31 @@ class RepurchaseDiscountTest $this->assert($newGoodsList[0]['repurchase_discount_money'] === 10.00, "商品 201 立减应为 10.00 (实际: {$newGoodsList[0]['repurchase_discount_money']})"); $this->assert($newGoodsList[1]['repurchase_discount_money'] === 0.00, "商品 202 立减应为 0.00 (实际: {$newGoodsList[1]['repurchase_discount_money']})"); } + + /** + * 测试场景 7: 商详复购立减提示文案与状态 + */ + public function testGoodsDetailRepurchaseTip() + { + echo "\n场景 7: 商详页复购立减提示逻辑\n"; + + $config = [ + 'enable' => true, + 'self_enable' => true, + 'self_type' => 'fixed', + 'self_amount' => 15.00, + 'self_rate' => 0 + ]; + + // 模拟已购买老客 + $discountDesc = ($config['self_type'] ?? 'fixed') === 'rate' ? ($config['self_rate'] . '%') : ('¥' . $config['self_amount']); + $tipRepurchase = '您曾购买过本商品,再次下单立减' . $discountDesc; + $tipNew = '老客复购享立减' . $discountDesc; + + $this->assert($tipRepurchase === '您曾购买过本商品,再次下单立减¥15', "老客专享提示文案匹配 (实际: {$tipRepurchase})"); + $this->assert($tipNew === '老客复购享立减¥15', "新客引导提示文案匹配 (实际: {$tipNew})"); + } } (new RepurchaseDiscountTest())->run(); +