我的购物车有2个功能,第一个返回的总价和商品的总数量:
Model.findOneAndDelete({},{"sort": { "_id": -1 }})
这两个函数返回这两个请求:
public function GetCartTotal(Request $request)
{
if (!Session::has('cart')) {
return Response::json([null]);
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return Response::json([
'totalPrice' => $cart->totalPrice,
'totalQty' => $cart->totalQty]
);
}
public function GetCartItems()
{
if (!Session::has('cart')) {
return Response::json(['items' => null]);
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return Response::json(['items' => $cart->items]);
}
和:
{
totalPrice: 320750000,
totalQty: 9
}
我可以通过此功能销毁所有购物车会话:
{
items:{
1:{
id:1,
qty:5,
price:750000,
item:{
id:1,
name:"Product title 1",
desc:null,
price:"150000",
barcode:"1556717299",
Image:"1556717299.jpg",
count:"25",
created_at:"2019-05-01 17:58:19",
updated_at:"2019-05-01 17:58:19"
}
},
2:{
id:2,
qty:4,
price:320000000,
item:{
id:2,
name:"Product title 2",
desc:null,
price:"80000000",
barcode:"1556729461",
Image:"1556729461.jpg",
count:"52",
created_at:"2019-05-01 21:21:01",
updated_at:"2019-05-01 21:21:01"
}
}
}
}
如何删除特定的购物车商品? 我正在使用此方法及其工作。但不会更改总价格和总数量:
public function RemoveAllCartItems(){
Session::forget('cart');
}
答案 0 :(得分:1)
您可以通过pulling -> updating -> putting
$cart = Session::pull('cart'); // Retrieve and delete an item
unset($cart->items[$id]);
// Add any additional logic that updates $cart (eg: updating qty and total price)
Session::put('cart', $cart);