問題: 當商品設置限購為 1 時,夠買數量未超過限購,但是還是提示超出限購數量
修復方法:
修改代碼路徑:
app\common\repositories\store\order\StoreOrderCreateRepository.php
修改代碼一:
$is_pays = array_unique(array_column($merchantCart['list'],'is_pay'));
if ( count($is_pays) > 1 || $is_pays[0] == 1) throw new ValidateException('存在已購買商品請重新添加購物車');
// 獲取每個商品的ID數組
$product_ids = array_column($merchantCart['list'],'product_id');
$num_data = array_column($merchantCart['list'],'cart_num');
$id_num = [];
// 獲取每個商品ID出現的次數及購買數量,作為驗證限購數量使用
foreach ($product_ids as $key => $value) {
$id_num[$value] = isset($id_num[$value]) ? $id_num[$value] + $num_data[$key] : $num_data[$key];
}
$countList = count($merchantCart['list']);
修改后的樣子:
修改代碼二:
$count = $id_num[$cart['product_id']];
if (($count) > $cart['product']['once_max_count']) {
throw new ValidateException('[超出限購總數:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...');
}
$pay_count = $storeOrderRepository->getMaxCountNumber($cart['uid'], $cart['product_id']);
if (($count + $pay_count) > $cart['product']['once_max_count']) {
throw new ValidateException('[超出限購總數:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...');
}
修改后的樣子:
最后:修改完了需要重啟swoole服務