优化复购功能:指定商品复购付款时直接立减(不返现),解耦分销买家佣金,纯后端实现
这个提交包含在:
@@ -88,6 +88,41 @@ trait OrderOper
|
||||
$user_address
|
||||
);
|
||||
|
||||
// 计算满足复购规则的指定商品的复购立减
|
||||
$repurchase_discount_money = 0.0;
|
||||
try {
|
||||
$repurchaseResult = \addons\shopro\library\commission\RepurchaseService::calculateOrderRepurchaseDiscounts(
|
||||
$new_goods_list,
|
||||
$user ? $user->id : 0
|
||||
);
|
||||
$new_goods_list = $repurchaseResult['goods_list'];
|
||||
$repurchase_discount_money = $repurchaseResult['total_repurchase_discount'];
|
||||
|
||||
// 将复购立减注入活动优惠列表,前端零改动原生展示
|
||||
if ($repurchase_discount_money > 0) {
|
||||
$repurchaseGoodsIds = [];
|
||||
foreach ($new_goods_list as $g) {
|
||||
if (!empty($g['repurchase_discount_money']) && $g['repurchase_discount_money'] > 0) {
|
||||
$repurchaseGoodsIds[] = $g['goods_id'];
|
||||
}
|
||||
}
|
||||
$activity_discount_infos[] = [
|
||||
'activity_id' => 'repurchase',
|
||||
'activity_title' => '复购立减',
|
||||
'activity_type' => 'repurchase',
|
||||
'activity_discount_money' => $repurchase_discount_money,
|
||||
'rule_type' => 'money',
|
||||
'discount_rule' => [
|
||||
'discount' => $repurchase_discount_money,
|
||||
],
|
||||
'goods_ids' => implode(',', $repurchaseGoodsIds)
|
||||
];
|
||||
$activity_discount_money = bcadd((string)$activity_discount_money, (string)$repurchase_discount_money, 2);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$repurchase_discount_money = 0.0;
|
||||
}
|
||||
|
||||
// 计算订单总金额,需支付金额
|
||||
list(
|
||||
$new_goods_list,
|
||||
@@ -105,7 +140,8 @@ trait OrderOper
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids,
|
||||
$coupon_money,
|
||||
$deliveryFee
|
||||
$deliveryFee,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
|
||||
// 获取发票金额
|
||||
@@ -150,7 +186,8 @@ trait OrderOper
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressList,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
}
|
||||
|
||||
@@ -242,7 +279,8 @@ trait OrderOper
|
||||
$invoice,
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money
|
||||
) {
|
||||
// 订单创建前
|
||||
$data = [
|
||||
@@ -260,6 +298,7 @@ trait OrderOper
|
||||
'coupon_fee' => $coupon_fee,
|
||||
'activity_discount_money' => $activity_discount_money,
|
||||
'dispatch_discount_money' => $dispatch_discount_money,
|
||||
'repurchase_discount_money' => $repurchase_discount_money,
|
||||
'activity_discount_infos' => $activity_discount_infos,
|
||||
'new_goods_list' => $new_goods_list,
|
||||
'activity_type' => $activity_type,
|
||||
@@ -348,6 +387,9 @@ trait OrderOper
|
||||
$orderData['platform'] = request()->header('platform');
|
||||
|
||||
$ext = $activity_discount_infos ? ['activity_discount_infos' => $activity_discount_infos] : []; // 促销活动信息
|
||||
if (!empty($repurchase_discount_money) && floatval($repurchase_discount_money) > 0) {
|
||||
$ext['repurchase_discount_money'] = $repurchase_discount_money;
|
||||
}
|
||||
$orderData['ext'] = json_encode($ext);
|
||||
$order = new Order();
|
||||
$order->allowField(true)->save($orderData);
|
||||
@@ -428,6 +470,9 @@ trait OrderOper
|
||||
if (isset($buyinfo['dispatch_phone'])) {
|
||||
$ext['dispatch_phone'] = $buyinfo['dispatch_phone'];
|
||||
}
|
||||
if (isset($buyinfo['repurchase_discount_money']) && floatval($buyinfo['repurchase_discount_money']) > 0) {
|
||||
$ext['repurchase_discount_money'] = $buyinfo['repurchase_discount_money'];
|
||||
}
|
||||
$orderItem->ext = json_encode($ext);
|
||||
$orderItem->save();
|
||||
}
|
||||
|
||||
@@ -717,7 +717,8 @@ trait OrderOperCreate
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids,
|
||||
$coupon_money,
|
||||
$deliveryFee
|
||||
$deliveryFee,
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// (搜这个找注释)加上运费,dispatch_amount 没有重新计算,和 origin_dispatch_amount 相同
|
||||
@@ -737,7 +738,8 @@ trait OrderOperCreate
|
||||
$activity_discount_infos,
|
||||
$activity_discount_money,
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids
|
||||
$free_shipping_goods_ids,
|
||||
$repurchase_discount_money
|
||||
);
|
||||
|
||||
return [
|
||||
@@ -814,7 +816,8 @@ trait OrderOperCreate
|
||||
$invoice_amount,
|
||||
$deliveryFee,
|
||||
$expressList,
|
||||
$expressName
|
||||
$expressName,
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// 需要处理小数点的数据
|
||||
@@ -829,6 +832,7 @@ trait OrderOperCreate
|
||||
"coupon_fee",
|
||||
"activity_discount_money",
|
||||
"dispatch_discount_money",
|
||||
"repurchase_discount_money",
|
||||
"invoice_amount",
|
||||
'deliveryFee'
|
||||
);
|
||||
@@ -882,7 +886,8 @@ trait OrderOperCreate
|
||||
$activity_discount_infos,
|
||||
$activity_discount_money,
|
||||
$dispatch_discount_money,
|
||||
$free_shipping_goods_ids = []
|
||||
$free_shipping_goods_ids = [],
|
||||
$repurchase_discount_money = 0
|
||||
)
|
||||
{
|
||||
// 如果邮费有优惠,分配邮费优惠的金额
|
||||
@@ -915,8 +920,8 @@ trait OrderOperCreate
|
||||
|
||||
if (floatval($activity_discount_money)) {
|
||||
foreach ($activity_discount_infos as $key => $info) {
|
||||
if ($info['activity_type'] == 'free_shipping') {
|
||||
// 上面已经单独处理过满包邮优惠
|
||||
if ($info['activity_type'] == 'free_shipping' || $info['activity_type'] == 'repurchase') {
|
||||
// 上面已经单独处理过满包邮优惠,复购立减优先按商品精准分配
|
||||
continue;
|
||||
}
|
||||
$current_discount_goods_ids = explode(',', $info['goods_ids']);
|
||||
@@ -946,8 +951,21 @@ trait OrderOperCreate
|
||||
}
|
||||
}
|
||||
|
||||
// 剩余要加权平均分配的优惠
|
||||
$last_discount_money = bcsub($discount_fee, bcadd($dispatch_discount_money, $activity_discount_money, 2), 2);
|
||||
// 如果存在复购立减,精准归属到满足复购的指定商品本身
|
||||
if (floatval($repurchase_discount_money)) {
|
||||
foreach ($new_goods_list as $key => $buyinfo) {
|
||||
$itemRepurchaseDiscount = $buyinfo['repurchase_discount_money'] ?? 0;
|
||||
if (floatval($itemRepurchaseDiscount) > 0) {
|
||||
$new_goods_list[$key]['discount_fee'] = bcadd($new_goods_list[$key]['discount_fee'], (string)$itemRepurchaseDiscount, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 剩余要加权平均分配的优惠(如优惠券),需剔除邮费优惠和全部活动优惠(含复购立减)
|
||||
$assigned_discount = bcadd($dispatch_discount_money, $activity_discount_money, 2);
|
||||
$last_discount_money = bcsub($discount_fee, $assigned_discount, 2);
|
||||
$last_discount_money = $last_discount_money > 0 ? $last_discount_money : 0;
|
||||
|
||||
// 计算每个商品分配到的优惠券的优惠金额,顺便计算出来 pay_price 的金额
|
||||
foreach ($new_goods_list as $key => $buyinfo) {
|
||||
$scale = 0; // 按照商品价格和总价计算每个 item 的比例
|
||||
@@ -959,7 +977,8 @@ trait OrderOperCreate
|
||||
$current_discount_fee = round(bcmul($last_discount_money, $scale, 3), 2);
|
||||
$new_goods_list[$key]['discount_fee'] = bcadd($new_goods_list[$key]['discount_fee'], $current_discount_fee, 2);
|
||||
// 每个商品除了运费之后分配的支付金额
|
||||
$new_goods_list[$key]['pay_price'] = bcsub($buyinfo['goods_amount'], $new_goods_list[$key]['discount_fee'], 2);
|
||||
$calc_pay_price = bcsub($buyinfo['goods_amount'], $new_goods_list[$key]['discount_fee'], 2);
|
||||
$new_goods_list[$key]['pay_price'] = $calc_pay_price < 0 ? '0.00' : $calc_pay_price;
|
||||
}
|
||||
|
||||
return $new_goods_list;
|
||||
|
||||
在新工单中引用
屏蔽一个用户