修复库存问题: 当购物车中商品加上详情页购买商品大于库存时,可下单成功

This commit is contained in:
Edward Yang 2023-03-03 16:52:12 +08:00
parent 3971ad5977
commit a1b1f6ba15
4 changed files with 46 additions and 26 deletions

View File

@ -18,19 +18,19 @@ class Asset
{
private $pluginPath;
const CONTENT_TYPES = [
'js' => 'application/javascript',
'css' => 'text/css',
'jpg' => 'image/jpeg',
public const CONTENT_TYPES = [
'js' => 'application/javascript',
'css' => 'text/css',
'jpg' => 'image/jpeg',
'apng' => 'image/apng',
'avif' => 'image/avif',
'gif' => 'image/gif',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'webp' => 'image/webp',
'webm' => 'video/webm',
'ogg' => 'video/ogg',
'ogg' => 'video/ogg',
];
public function __construct($pluginCode)
@ -38,7 +38,7 @@ class Asset
if (empty($pluginCode)) {
throw new \Exception('Empty plugin code!');
}
$folderName = Str::studly($pluginCode);
$folderName = Str::studly($pluginCode);
$this->pluginPath = base_path('plugins/' . $folderName);
}
@ -47,7 +47,6 @@ class Asset
return new self($pluginCode);
}
/**
* Get content and type
*
@ -59,11 +58,13 @@ class Asset
$filePath = $this->pluginPath . '/Static/' . $file;
if (is_file($filePath)) {
$extension = File::extension($filePath);
return [
'type' => self::CONTENT_TYPES[$extension] ?? '',
'type' => self::CONTENT_TYPES[$extension] ?? '',
'content' => file_get_contents($filePath),
];
}
return [];
}
}

View File

@ -54,23 +54,27 @@ class CartController extends Controller
*/
public function store(CartRequest $request)
{
$skuId = $request->sku_id;
$quantity = $request->quantity ?? 1;
$buyNow = (bool) $request->buy_now ?? false;
$customer = current_customer();
try {
$skuId = $request->sku_id;
$quantity = $request->quantity ?? 1;
$buyNow = (bool) $request->buy_now ?? false;
$customer = current_customer();
$sku = ProductSku::query()
->whereRelation('product', 'active', '=', true)
->findOrFail($skuId);
$sku = ProductSku::query()
->whereRelation('product', 'active', '=', true)
->findOrFail($skuId);
$cart = CartService::add($sku, $quantity, $customer);
if ($buyNow) {
CartService::select($customer, [$cart->id]);
$cart = CartService::add($sku, $quantity, $customer);
if ($buyNow) {
CartService::select($customer, [$cart->id]);
}
$cart = hook_filter('cart.store.data', $cart);
return json_success(trans('shop/carts.added_to_cart'), $cart);
} catch (\Exception $e) {
return json_fail($e->getMessage());
}
$cart = hook_filter('cart.store.data', $cart);
return json_success(trans('shop/carts.added_to_cart'), $cart);
}
/**

View File

@ -21,13 +21,15 @@ class PluginController extends Controller
$contents = Asset::getInstance($code)->getContent($path);
$content = $contents['content'] ?? '';
$type = $contents['type'] ?? '';
$type = $contents['type'] ?? '';
if ($content && $type) {
$response = Response::make($content);
$response->header('Content-Type', $type);
return $response;
}
return '';
}
}

View File

@ -40,6 +40,13 @@ class CartService
$item->delete();
}
$cartQuantity = $item->quantity;
$skuQuantity = $item->sku->quantity;
if ($cartQuantity > $skuQuantity) {
$item->quantity = $skuQuantity;
$item->save();
}
return $description && $product;
});
@ -82,6 +89,12 @@ class CartService
]);
}
$cartQuantity = $cart->quantity;
$skuQuantity = $cart->sku->quantity;
if ($cartQuantity > $skuQuantity) {
throw new \Exception(trans('cart.stock_out'));
}
return $cart;
}