简体   繁体   English

使用 Laravel 添加到购物车时删除手头的物品

[英]Delete items on hand when added to cart with Laravel

I am building a small e-commerce site out and want to be able to have the stock on hand be updated when items are added or deleted from someone's cart.我正在建立一个小型电子商务网站,并希望能够在某人的购物车中添加或删除商品时更新手头的库存。 In my cart controller, I have four functions: one to add an item, one to increase the item, one to decrease the item, and one to remove an item.在我的购物车 controller 中,我有四个功能:一个添加项目,一个增加项目,一个减少项目,一个删除项目。 Here is the code for that:这是代码:

     public function addItemToCart(Request $request, $id) {

    $prevCart = $request->session()->get('cart');
    $cart = new Cart($prevCart);

    $product = Product::find($id);
    $cart->addItem($id, $product);
    $request->session()->put('cart', $cart);

    return redirect()->route("home");

}

public function increaseSingleProduct(Request $request, $id) {

    $prevCart = $request->session()->get('cart');
    $cart = new Cart($prevCart);

    $product = Product::find($id);
    $cart->addItem($id, $product);
    $request->session()->put('cart', $cart);

    return redirect()->back();
}

public function decreaseSingleProduct(Request $request, $id) {

    $prevCart = $request->session()->get('cart');
    $cart = new Cart($prevCart);

    if( $cart->items[$id]['quantity'] > 1) {
        $product = Product::find($id);
        $cart->items[$id]['quantity'] = $cart->items[$id]['quantity']-1;
        $cart->items[$id]['totalSinglePrice'] = $cart->items[$id]['quantity'] * $product['price'];
        $cart->updatePriceAndQunatity();

        $request->session()->put('cart', $cart);
    }
    return redirect()->back();
}

public function deleteItemFromCart(Request $request, $id) {

    $cart = Session::get('cart');

    if(array_key_exists($id, $cart->items)){
        unset($cart->items[$id]);
    }
    $prevCart = $request->session()->get("cart");
    $updatedCart = new Cart($prevCart);
    $updatedCart->updatePriceAndQunatity();

    $request->session()->put("cart",$updatedCart);

    return redirect()->route('shopping cart'); 
}

The quantity here does not refer to the quantity on hand, it refers to the quantity in the cart.这里的数量不是指手头的数量,是指购物车中的数量。 In the products table, I have a column that is called stock_quantity.在产品表中,我有一个名为 stock_quantity 的列。 Ideally, this is what I would like to get updated as each function is performed.理想情况下,这是我希望在执行每个 function 时更新的内容。

I may not be getting the complexity of your question.我可能没有得到你问题的复杂性。 Don't you just have to find the product and increment or decrement?你不只需要找到产品并增加或减少吗?

$product = Product::findOrFail($item_id);
$product->stock_quantity++; // or --
$product->save();

I wouldn't be doing this as you add to the cart though.不过,当您添加到购物车时,我不会这样做。 I'd do it on checkout instead so that you don't leave your stock committed with an abandoned cart.我会在结账时这样做,这样您就不会将库存留在废弃的购物车中。 Alternatively, if you really need to commit your stock while someone has something in the cart, implement a function to remove the committed stock if it's abandoned.或者,如果您确实需要在有人在购物车中有东西时提交您的库存,请实施 function 以删除已放弃的已提交库存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM