优化:商品详情加入购物车、立即购买、购物车页面下单 - 添加批量售卖商品下单及加入购物车处理。不符合条件禁止下单或者加入购物车
This commit is contained in:
parent
525a6c1740
commit
bfc31e06c4
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Beike\Shop\Http\Controllers;
|
||||
|
||||
use Beike\Models\Product;
|
||||
use Beike\Repositories\OrderRepo;
|
||||
use Beike\Shop\Services\CheckoutService;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -22,6 +23,41 @@ class CheckoutController extends Controller
|
|||
try {
|
||||
$data = (new CheckoutService)->checkoutData();
|
||||
$data = hook_filter('checkout.index.data', $data);
|
||||
// 判断:当前购物车商品是否符合下单条件
|
||||
$carts = $data['carts']['carts'] ?? [];
|
||||
$cartQuantity = collect($carts)->groupBy('product_id')->map(function($productGroup){
|
||||
$first = $productGroup->first();
|
||||
return [
|
||||
'product_id' => $first['product_id'],
|
||||
'total_quantity' => $productGroup->sum('quantity'),
|
||||
'name_format' => $first['name_format'],
|
||||
'minimum_order' => $first['minimum_order'],
|
||||
'sales_method' => $first['sales_method'],
|
||||
'piece_to_batch' => $first['piece_to_batch'],
|
||||
];
|
||||
})->toArray();
|
||||
foreach($carts as $cartItem){
|
||||
$productInfo = $cartQuantity[$cartItem['product_id']];
|
||||
// 判断:起订量 如果大于购买数量 不符合下单条件
|
||||
$minimumOrder = $productInfo['sales_method'] == 'batches' ? ($productInfo['minimum_order'] * $productInfo['piece_to_batch']) : $productInfo['minimum_order'];// 起订量
|
||||
if($minimumOrder > $productInfo['total_quantity']){
|
||||
throw new \Exception(trans('product.quantity_error_mini',[
|
||||
'goods_name'=>$productInfo['name_format'],
|
||||
'num'=>$minimumOrder,
|
||||
]));
|
||||
break;
|
||||
}
|
||||
// 判断:批量销售商品 购买总数量必须是N的倍数 否则不符合下单条件
|
||||
$pieceToBatch = $productInfo['sales_method'] == 'batches' ? $productInfo['piece_to_batch'] : 1;// 倍数
|
||||
if(($productInfo['total_quantity'] % $pieceToBatch) != 0){
|
||||
throw new \Exception(trans('product.quantity_error_multiple',[
|
||||
'goods_name'=>$productInfo['name_format'],
|
||||
'num'=>$pieceToBatch,
|
||||
]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return view('checkout', $data);
|
||||
} catch (\Exception $e) {
|
||||
|
|
|
|||
|
|
@ -39,8 +39,8 @@ class CartDetail extends JsonResource
|
|||
'product_sku' => $skuCode,
|
||||
'name' => $productName,
|
||||
'name_format' => sub_string($productName),
|
||||
'unit' => $unit,
|
||||
'unit_format' => $unit,
|
||||
'unit' => $unit,
|
||||
'unit_format' => $unit,
|
||||
'image' => $image,
|
||||
'image_url' => image_resize($image),
|
||||
'quantity' => $this->quantity,
|
||||
|
|
@ -51,7 +51,10 @@ class CartDetail extends JsonResource
|
|||
'subtotal' => $subTotal,
|
||||
'subtotal_format' => currency_format($subTotal),
|
||||
'variant_labels' => trim($sku->getVariantLabel()),
|
||||
'active' => $product->active
|
||||
'active' => $product->active,
|
||||
'minimum_order' => $product->minimum_order,
|
||||
'sales_method' => $product->sales_method,
|
||||
'piece_to_batch' => $product->piece_to_batch
|
||||
];
|
||||
|
||||
return hook_filter('resource.cart.detail', $result);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
"devDependencies": {
|
||||
"axios": "^0.21",
|
||||
"bootstrap": "^5.2.2",
|
||||
"laravel-mix": "^6.0.6",
|
||||
"laravel-mix": "^6.0.49",
|
||||
"lodash": "^4.17.19",
|
||||
"resolve-url-loader": "^4.0.0",
|
||||
"sass": "^1.38.1",
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
|
|
@ -96,7 +96,9 @@ $(function () {
|
|||
|
||||
$http.put(`/carts/${id}`, {quantity: quantity, sku_id}, {hload: true}).then((res) => {
|
||||
updateMiniCartData(res);
|
||||
})
|
||||
}).catch((err) => {
|
||||
console.log("修改购物车信息header - 错误:",err);
|
||||
});
|
||||
})
|
||||
|
||||
function updateMiniCartData(res) {
|
||||
|
|
|
|||
|
|
@ -11,21 +11,27 @@ $(document).on('click', '.quantity-wrap .right i', function(event) {
|
|||
event.preventDefault();
|
||||
|
||||
let input = $(this).parent().siblings('input')
|
||||
let minimum_order = input.attr('minimum') || 0;// 最小起订量
|
||||
let sales_method = input.attr('sales_method') || 'piece'// 销售方式:piece=按件卖,batches=按批卖
|
||||
let piece_to_batch = input.attr('piece_to_batch') || 1;// 按批卖,每批等于多少件
|
||||
let step = sales_method === 'piece' ? parseInt(1) : parseInt(piece_to_batch);// 点击后加减数量
|
||||
|
||||
if ($(this).hasClass('bi-chevron-up')) {
|
||||
input.val(input.val() * 1 + 1)
|
||||
input.val(input.val() * 1 + step)
|
||||
input.get(0).dispatchEvent(new Event('input'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.val() * 1 <= input.attr('minimum') * 1) {
|
||||
input.val(0)
|
||||
input.get(0).dispatchEvent(new Event('input'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.val () * 1 <= 1) {
|
||||
if (input.val () * 1 <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
input.val(input.val() * 1 - 1)
|
||||
input.val(input.val() * 1 - step)
|
||||
input.get(0).dispatchEvent(new Event('input'));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -41,4 +41,6 @@ return [
|
|||
'sales_method_piece_unit' => 'Sell by :unit',
|
||||
'total_num' => ':num :unit in total',
|
||||
'multiple_error' => 'The purchase quantity must be a multiple of :num',
|
||||
'quantity_error_mini' => 'Purchase quantity error, the total purchase quantity of [:goods_name] must be greater than or equal to [:num]',
|
||||
'quantity_error_multiple' => 'Purchase quantity error, the total purchase quantity of [:goods_name] must be a multiple of [:num]',
|
||||
];
|
||||
|
|
|
|||
|
|
@ -42,4 +42,10 @@ return [
|
|||
'sales_method_piece_unit' => '按:unit卖',
|
||||
'total_num' => '共:num:unit',
|
||||
'multiple_error' => '采购数量必须是:num的倍数',
|
||||
'quantity_error_mini' => '采购数量错误,【:goods_name】的采购总数量必须大于等于【:num】',
|
||||
'quantity_error_multiple' => '采购数量错误,【:goods_name】的采购总数量必须是【:num】的倍数',
|
||||
|
||||
|
||||
|
||||
|
||||
];
|
||||
|
|
|
|||
|
|
@ -58,7 +58,15 @@
|
|||
</td>
|
||||
<td>
|
||||
<div class="quantity-wrap">
|
||||
<input type="text" class="form-control" @input="quantityChange(product.quantity, product.cart_id, product.sku_id)" onkeyup="this.value=this.value.replace(/\D/g,'')" v-model.number="product.quantity" name="quantity" minimum="0">
|
||||
<input type="text"
|
||||
class="form-control"
|
||||
@input="quantityChange(product.quantity, product.cart_id, product.sku_id)"
|
||||
onkeyup="this.value=this.value.replace(/\D/g,'')"
|
||||
v-model.number="product.quantity"
|
||||
name="quantity"
|
||||
:sales_method="product.sales_method"
|
||||
:piece_to_batch="product.piece_to_batch"
|
||||
minimum="0">
|
||||
<div class="right">
|
||||
<i class="bi bi-chevron-up"></i>
|
||||
<i class="bi bi-chevron-down"></i>
|
||||
|
|
@ -165,7 +173,9 @@
|
|||
const self = this;
|
||||
$http.put(`/carts/${cart_id}`, {quantity: quantity, sku_id}, {hload: true}).then((res) => {
|
||||
this.setUpdateData(res);
|
||||
})
|
||||
}).catch((err) => {
|
||||
console.log("修改购物车信息cart - 错误:",err);
|
||||
});
|
||||
},
|
||||
|
||||
checkedBtnDelete(cart_id) {
|
||||
|
|
|
|||
|
|
@ -301,6 +301,9 @@
|
|||
onkeyup="this.value=this.value.replace(/\D/g,'')"
|
||||
v-model="value.quantity"
|
||||
value="0"
|
||||
:minimum="minimum_order"
|
||||
:sales_method="sales_method"
|
||||
:piece_to_batch="piece_to_batch"
|
||||
:name="'variables['+ variable_index +'].value['+value_index+'].quantity'">
|
||||
<div class="right">
|
||||
<i class="bi bi-chevron-up" @click="quantityChange"></i>
|
||||
|
|
@ -318,6 +321,9 @@
|
|||
:disabled="!product.quantity"
|
||||
:value="quantity"
|
||||
@input="singleQuantityChange($event)"
|
||||
:minimum="minimum_order"
|
||||
:sales_method="sales_method"
|
||||
:piece_to_batch="piece_to_batch"
|
||||
name="quantity">
|
||||
<div class="right" v-if="!isNumSelect">
|
||||
<i class="bi bi-chevron-up"></i>
|
||||
|
|
@ -345,6 +351,9 @@
|
|||
onkeyup="this.value=this.value.replace(/\D/g,'')"
|
||||
:value="add_buy_sku[skuIndex].quantity"
|
||||
@input="skuQuantityChange($event,skuIndex)"
|
||||
:minimum="minimum_order"
|
||||
:sales_method="sales_method"
|
||||
:piece_to_batch="piece_to_batch"
|
||||
name="quantity">
|
||||
<div class="right" v-if="!isNumSelect">
|
||||
<i class="bi bi-chevron-up"></i>
|
||||
|
|
@ -967,8 +976,6 @@
|
|||
let _this = this;
|
||||
let stock = _this.add_buy_sku[skuIndex].stock || 0;
|
||||
let quantity = event.target.value || 0;
|
||||
// 处理批量购买操作
|
||||
if(_this.sales_method === 'batches') quantity = _this.computeQuantity(quantity,_this.add_buy_sku[skuIndex].quantity);
|
||||
// 判断是否超过库存
|
||||
quantity = quantity > stock ? stock : quantity;// 不能超过库存
|
||||
_this.add_buy_sku[skuIndex].quantity = typeof quantity != 'number' ? quantity.replace(/\D/g, '') : quantity;
|
||||
|
|
@ -982,35 +989,12 @@
|
|||
let _this = this;
|
||||
let stock = _this.product.quantity || 0;// 库存
|
||||
let quantity = event.target.value || 0;// 购买数量
|
||||
// 处理批量购买操作
|
||||
if(_this.sales_method === 'batches') quantity = _this.computeQuantity(quantity,_this.quantity);
|
||||
// 判断是否超过库存
|
||||
quantity = quantity > stock ? stock : quantity;// 不能超过库存
|
||||
_this.quantity = typeof quantity != 'number' ? quantity.replace(/\D/g,'') : quantity;
|
||||
|
||||
_this.$forceUpdate();
|
||||
},
|
||||
// 处理批量购买的增减
|
||||
computeQuantity(quantity,oldQuantity){
|
||||
let _this = this;
|
||||
// 判断:当前操作是增加还是减少
|
||||
quantity = typeof quantity != 'number' ? quantity.replace(/\D/g,'') : quantity;
|
||||
oldQuantity = typeof oldQuantity != 'number' ? oldQuantity.replace(/\D/g,'') : oldQuantity;
|
||||
let status = oldQuantity < quantity;// 是添加还是减少:旧数量少于当前数量=添加;旧数量大于当前数量=减少
|
||||
let diff = oldQuantity - quantity;// 新旧数据之间的差距
|
||||
if(parseInt(diff) == 1 || parseInt(diff) == -1){
|
||||
// 点击操作按钮 增减1
|
||||
console.log('点击操作按钮 增减1');
|
||||
if(status) quantity = parseInt(quantity) + parseInt(_this.piece_to_batch) - parseInt(1);
|
||||
else quantity = parseInt(quantity) - parseInt(_this.piece_to_batch) + parseInt(1);
|
||||
}else{
|
||||
// 输入数字 增减数量不确定
|
||||
console.log('输入数字 增减数量不确定');
|
||||
|
||||
}
|
||||
|
||||
return quantity;
|
||||
},
|
||||
// 计算当前订单总额
|
||||
computeOrderMoney(){
|
||||
let _this = this;
|
||||
|
|
|
|||
Loading…
Reference in New Issue