When I remove my items from my cart page the item gets removed from the cart but the cart badge that I have next to my menu still has a value instead of nothing.
For Example:
I add two products to my cart. My cart badge now says 2. So I now go to my cart page and remove both items, but my cart badge still says 2. Or if I just remove 1 item, instead of the cart badge changing to 1 it still says 2.
My menu.blade.php
<a href="{{ route('product.shoppingCart') }}">
cart
<span class="badge">
{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}
</span>
</a>
my cart.blade.php
@extends('layouts.public')
@section('content')
<div class="content_wrapper">
@if(Session::has('cart'))
<div class="container">
<div class="cart_table">
<div class="table">
<div class="thead">
<div class="tr">
<div class="th border-bottom">Image</div>
<div class="th border-bottom"> </div>
<div class="th border-bottom">Qty</div>
<div class="th border-bottom">Unit Price</div>
<div class="th border-bottom">Total Price</div>
<div class="th border-bottom"></div>
</div>
</div>
<div class="tbody">
@foreach($products as $product)
<?php
$image = getImagesArray($product['item']['image']);
?>
<div class="tr">
<div class="td border-bottom" data-title="Image">
@if(!empty($image))
<img src={!! asset("product_images/thumbs/$image[0]") !!}>
@endif
</div>
<div class="td border-bottom" data-title="Title">
<strong>{{ $product['item']['title'] }}</strong>
</div>
<div class="td border-bottom" data-title="Qty">
<div class="quantity_box">
<div class="qty_number">
<input type="text" value="{{ $product['qty'] }}">
<div class="inc button">
<a href="{{ route('product.addToCart', ['id' => $product['item']['id']]) }}" class="">
+
</a>
</div>
<div class="dec button">
<a href="{{ route('product.removeFromCart', ['id' => $product['item']['id']]) }}" class="">
-
</a>
</div>
</div>
</div>
</div>
<div class="td border-bottom" data-title="Unit Price">
<span class="label label-success">R {{ $product['item']['price'] }}</span>
</div>
<div class="td border-bottom" data-title="Total Price">
<span class="label label-success">R {{ $product['price'] }}</span>
</div>
</div>
@endforeach
</div>
</div>
</div>
<div class="cart_subtotal text-right">
<div class="subtotal_text">
Subtotal
</div>
<div class="subtotal_price text-right">
{{ $totalPrice }}
</div>
</div>
</div>
@else
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<h2>No items in cart!</h2>
</div>
</div>
@endif
</div>
@stop
Cart.php
<?php
namespace App;
class Cart
{
public $items = null;
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
if($oldCart)
{
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function removeItem($id)
{
$this->items[$id]['qty']--;
$this->items[$id]['price'] -= $this->items[$id]['item']['price'];
$this->totalPrice -= $this->items[$id]['item']['price'];
if($this->items[$id]['qty'] <= 0)
{
unset($this->items[$id]);
}
}
}
my controller
public function removeItem($id)
{
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->removeItem($id);
Session::put('cart', $cart);
return redirect()->back();
}