Laravel 添加到购物车按钮 将相同产品添加到购物车 +1 数量

Laravel add to cart button with same product into cart with +1 quantity

我是 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');

} 
});
}
 });
 
 
});

无论何时执行 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
    ]);
            
}