i am making an e commerce shopping cart where i have used session to store cart item. But the problem is when i am trying to update (increase or decrease) cart item's quantity i am not getting the accurate total price. i am trying to update the whole cart not by single item...
This is update function in cart Model
public function update($id, $qty){
//reset qty and price in the cart
$this->totalQty -= $this->items[$id]['qty'];
$this->totalPrice -= $this->items[$id]['price'] * $this->items[$id]['qty'];
//add item with new qty
$this->items[$id]['qty'] = $qty;
//total price and total qty in cart
$this->totalQty += $qty;
$this->totalPrice += $this->items[$id]['price'] * $qty;
}
This is update function in controller
//update cart
public function updateQty(Request $request){
$id = $request->item_id;
$qty = $request->qty;
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
for ($i=0; $i <count($id) ; $i++) {
$cart->update($id[$i], $qty[$i]);
Session::put('cart', $cart);
}
return redirect()->back();
}