Laravel 将相同产品的添加到购物车按钮添加到购物车,数量 +1
Posted
技术标签:
【中文标题】Laravel 将相同产品的添加到购物车按钮添加到购物车,数量 +1【英文标题】:Laravel add to cart button with same product into cart with +1 quantity 【发布时间】:2021-04-04 15:00:03 【问题描述】:我是 Laravel 的新手。添加到购物车按钮可将相同的产品添加到购物车中,数量为 +1。 试图检查库存是否大于数量。当一件商品的数量为 3 件而不是 2 件时,我想显示一些文本“请求的数量不可用”而不是表单提交。这可能吗?
总库存有 2 个。
问题是更多的添加到购物车到相同的产品到 +1 数量。没有什么能阻止添加到购物车“请求的数量不可用”。谁能帮帮我。
在控制器中
public function add_to_cart(Request $request)
$name = $request->product_name;
$price = $request->product_price;
$itemno = $request->product_itemno;
$qty = $request->product_quantity;
$color = $request->product_color;
$size = $request->product_size;
$stock = products::join('stocks', 'stocks.pid', 'products.id')
->join('sizes', 'sizes.pid', 'products.id')
->where([['products.item_no', '=', $itemno],['sizes.size_name', '=', $size]])
->first();
// Current stock quantity
$stockqty = $stock->qty;
if($qty <= $stockqty)
echo "<p>was successfully added to your shopping cart</p>";
Cart::add([
'id' => $itemno,
'weight' => 500,
'name' => $name,
'price' => $price,
'qty' => $qty,
'color' => $color,
'size' => $size
]);
else
echo "<p>The Requested quantity for this product is not available.</p>";
在 Ajax 中
$(document).ready(function()
$(document).on('click','#add_to_cart',function (e)
var cart_count = $('.navbar-tool-badge').text();
cart_count = parseInt(cart_count);
if($('input[type=radio][name=size]:checked').length == 0)
$('.msg').html('Please choose size.');
return false;
else
var product_name = $('#hidden-name').val();
var product_price = $('#hidden-price').val();
var product_itemno = $('#itemno').val();
var product_quantity = $('.quantity').val();
var product_color = $('#color').val();
var product_size = $("input[name='size']:checked").val();
e.preventDefault();
$.ajax
(
method:"POST",
url: " route('add_to_cart')",
data:
"_token": " csrf_token() ",
product_name:product_name,
product_price:product_price,
product_itemno:product_itemno,
product_quantity:product_quantity,
product_color:product_color,
product_size:product_size,
cache: false,
success: function(response)
$("#getCode").html(response);
$("#myModal").modal('show');
);
);
);
【问题讨论】:
那么问题出在哪里?您可以将echo
替换为return response()->json(['message' => 'Some message']);
并在前端处理消息以显示给客户
echo 或 response()-> 类似。向 ajax 调用发送数据时最好使用 echo。数量 4 使用会话。总库存 2。我想防止将具有相同产品的购物车添加到购物车中,数量比总库存 2 增加 1。如显示:“请求的数量不可用”。为了获得成功的消息,它应该总是比数量大。开始添加到购物车,数量比总库存 2.like display: 已成功添加到您的购物车
所以您正在检查if($qty <= $stockqty)
对吗?那么什么不起作用
$qty 与当前会话无关。 $qty 获取输入字段编号的值。我想检查如何使数量(当前会话)少于总库存。
您是否存储会话中每个产品的当前购物车数量?如果是这样,您可以通过密钥从会话中获取值。如果没有,您可以在进行 ajax 调用时将购物车信息传回 add_to_cart
函数
【参考方案1】:
无论何时执行add_to_cart
,您都可以将添加到购物车的每个产品数量的值放入会话中。像下面这样的东西应该可以工作。
public function add_to_cart(Request $request)
//You should get only the id of the product and cart from the frontend
//via ajax request
//Then you should fetch the Product record from database for that id
$product = Product::findOrFail($request->pid);
$cart = Cart::findOrFail($request->cid);
$sessionKey = "Cart-$cart->id-$product->id";
$session = $request->session();
$requestedQty = $request->product_quantity;
$desiredQty = 0;
//Get the stock data
$stock = products::join('stocks', 'stocks.pid', 'products.id')
->join('sizes', 'sizes.pid', 'products.id')
->where([
['products.item_no', '=', $itemno],
['sizes.size_name', '=', $size]
])
->first();
// Current stock quantity
$stockqty = $stock->qty;
//If the requested quantity is greater than the available stock
//when the product is added for the first time, send out of stock
if($requestedQty > $stockqty)
echo "<p>The Requested quantity for this product is not available.</p>";
//When a product is added for the first time session won't have entry
if(empty($session->get($sessionKey))
$desiredQty = $requestedQty;
$session->put($sessionKey, $requestedQuantity);
else
$desiredQty = $session->get($sessionKey) + $requestedQty;
if($desiredQty > $stockqty)
echo "<p>The Requested quantity for this product is not available.</p>";
//Overwrite the session entry with the new quantity
$session->put($sessionKey, $desiredQty);
echo "<p>was successfully added to your shopping cart</p>";
Cart::add([
'id' => $itemno,
'weight' => 500,
'name' => $name,
'price' => $price,
'qty' => $requestedQty,
'color' => $color,
'size' => $size
]);
【讨论】:
以上是关于Laravel 将相同产品的添加到购物车按钮添加到购物车,数量 +1的主要内容,如果未能解决你的问题,请参考以下文章
[我想使用laravel 6.0单击添加到卡片按钮时将商品显示到购物车中?
自定义添加到购物车按钮以将多个产品添加到购物车中:woocommerce
如果 Woocommerce 产品价格为零,则显示联系按钮,否则显示添加到购物车按钮