$form['province_id'], 'city_id' => $form['city_id'], 'district_id' => $form['district_id'], 'province' => $form['province'], 'city' => $form['city'], 'district' => $form['district'], 'town' => $form['town'] ?? '', 'town_id' => $form['town_id'] ?? 0, 'community' => $form['community'] ?? '', 'community_id' => $form['community_id'] ?? 0, 'detail' => $form['address'], 'name' => $form['name'], 'mobile' => $form['mobile'], ]; // 收货地址 $item['mch_order_info']['province'] = $address['province_id']; $item['mch_order_info']['city'] = $address['city_id']; $item['mch_order_info']['area'] = $address['district_id']; $item['mch_order_info']['town'] = $address['town_id'] ?? 0; $item['mch_order_info']['community'] = $address['community_id'] ?? 0; $item['mch_order_info']['region_name'] = $address['province'] .' '. $address['city'] .' '. $address['district'].' '.$address['town'].' '.$address['community']; $item['mch_order_info']['address'] = $address['detail']; $item['mch_order_info']['receiver_name'] = $address['name']; $item['mch_order_info']['mobile'] = $address['mobile']; $shipping_fee = 0;// 订单总运费初始化 $group_order_fee = 0;// 单个商户订单运费 $is_free = false;// 是否免邮 // 获取包邮地区 $feeShippingRules = FreeShippingRules::getFreeShippingRulesArea($item['mch_order_info']['mall_id'], $item['mch_order_info']['mch_id']); // 获取运费模板 $freightRules = FreightRules::getFreightRulesArea($item['mch_order_info']['mall_id'], $item['mch_order_info']['mch_id']); // 包邮计算 foreach ($feeShippingRules as $fee) { // 如果包邮金额<=购买商品金额,并且地区符合收货地址则免邮 if ($fee['money'] <= $item['mch_order_info']['goods_price'] && self::isInArea($address, $fee['district_ids'])) { $is_free = true; continue; } } // 不免邮处理 if ($is_free == false) { foreach ($item['goods'] as $good_id => $good) { $good_num = 0;// 第N个商品 if ($good['num'] > 1) { $good_num+= $good['num']; } else { $good_num++; } // 物流方式 if (!in_array(ShippingTypeEnum::LOGISTICS, explode(',', $form['sku'][$good_id]['goods']['freight_type']))) continue; // 计算满件包邮 if ($form['sku'][$good_id]['goods']['free_shipping_num'] > 0 && $good['num'] >= $form['sku'][$good_id]['goods']['free_shipping_num']) continue; // 计算满元包邮 if ($form['sku'][$good_id]['goods']['free_shipping_money'] > 0 && $good['goods_price'] >= $form['sku'][$good_id]['goods']['free_shipping_money']) continue; // 运费规则 if ($form['sku'][$good_id]['goods']['freight_rules_type'] == 1) { $thisRules = $freightRules[$form['sku'][$good_id]['goods']['freight_id']];// 根据商品id获取对应的规则 if(!empty($thisRules['rule'])){ foreach ($thisRules['rule'] as $rule) { // 获取该模板的所有地区id $areaArr = array_column($rule['list'], 'id'); // 判断收货地址是否在模板中 if (self::isInArea($address, $areaArr)) { if ($thisRules['type'] == 1) {// 按重计费 $new_num_weight = $form['sku'][$good_id]['weight'] * $good['num']; } else {// 按件计费 $new_num_weight = $good['num']; } // 计算[首件/首重]收费 if ($new_num_weight >= $rule['first']) { $group_order_fee += $rule['firstPrice']; $shipping_fee += $rule['firstPrice']; } if ($thisRules['type'] == 1) {// 计算续重收费 $surplus_weight = $new_num_weight - $rule['first'];// 剩余重量 = 总重量 - 首重重量 if ($surplus_weight > 0) { // 续重运费 = (剩余重量 / 续件重量)[向上取整] * 续重费用 [向下取整] if(isset($rule['second'])&&$rule['second']>0) { $fee = floor(ceil($surplus_weight / $rule['second']) * $rule['secondPrice']); $shipping_fee += $fee; $group_order_fee += $fee; } } } else {// 计算续件收费 $surplus_num = $good_num - $rule['first'];// 减扣首件数量 if($rule['second']>0){ if ($surplus_num > 0 && $rule['second']>0 && $surplus_num % $rule['second'] == 0) { // 续费 * (减扣首件数量 / 续件数量)[向下取整] if(isset($rule['second'])&&$rule['second']>0){ $fee = $rule['secondPrice'] * floor($surplus_num / $rule['second']); $shipping_fee += $fee; $group_order_fee += $fee; } } } } continue;// 已计算好运费则跳出循环 } } } } else { // 自定义运费 if ($form['sku'][$good_id]['goods']['shipping_fee'] >= 0) { $shipping_fee+= $form['sku'][$good_id]['goods']['shipping_fee']; $group_order_fee+= $form['sku'][$good_id]['goods']['shipping_fee']; } } } // 单个商户订单运费计算 $item['mch_order_info']['shipping_type'] = $form['data']['shipping_type'] ?? ShippingTypeEnum::LOGISTICS; $item['mch_order_info']['shipping_money'] = $group_order_fee; } // 运费赋值 $item['mch_order_info']['pay_money'] = $item['mch_order_info']['pay_money'] + $shipping_fee; $prev_shipping_fee = $form->marketing['deduct']['shipping_fee']['money'] ?? 0; $form->marketing['deduct']['shipping_fee'] = ['name' => '运费', 'money' => $prev_shipping_fee + $shipping_fee]; return $item; } /** * 判断收货地址是否在地区中 * @Author: lun * @DateTime: 2021/9/30 0030 14:45 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $address * @param $areaArr * @return bool */ private static function isInArea($address, $areaArr) { $return = false; if(in_array($address['province_id'], $areaArr) || in_array($address['city_id'], $areaArr) || in_array($address['district_id'], $areaArr)) $return = true; return $return; } }