Creating a Simple Shopping Cart Application - Step 3 - Loading the number of Cart Items from the session and clearing the cart

The cart is now working well and the cart contents are stored in a session so even if the user leaves the page the information will not be lost. When we created the page in step 1 we included a div called 'cart' which would indicate the number of items in the cart this was hard coded to 0. Now that we have a session we can change this to show the number of items from the session.

To do this first edit the file resources/views/products/displaygrid.blade.php. Locate the div with id='cart' at the top of the page. Place the blade variable {{$totalitems}} which contains the total number of products in the cart inside the div

$totalItems is a view variable which we need to set in the action which corresponds to this view in the productsController. Edit the file /app/Http/controllers/productsController.php and locate the displaygrid() function. Add the following code which figures out how many items are in the cart and then passes that information to the view via the $totalitems view variable.

    if ($request->session()->has('cart')) {
        $cart = $request->session()->get('cart');
        print_r($cart);
        $totalQty=0;
        foreach ($cart as $product => $qty) {
            $totalQty = $totalQty + $qty;
        }
        $totalItems=$totalQty;
    }
    else {
        $totalItems=0;
        echo "no cart";

    }
    return view('products.displaygrid')->with('products',$products)->with('totalItems',$totalItems);

This function needs to check the number of items in the cart (if there are any) and set the view variable appropriately. It then loads all the products from the database into a view variable called products before rendering the view.

Now, even if the user's browser crashes and they have to restart their browser the number of items in their cart will be reloaded from the session.

Clearing the cart is relatively straightforward. You just need a function in the controller which will remove the 'cart' session variable from the session. Edit the /app/Http/controllers/productsController.php file and add the following function.

     public function emptycart()
{
if (Session::has('cart')) {
Session::forget('cart');
}
return Response::json(['success'=>true],200);
}

If we have a new function in the controller then we must remember to include a corresponding route. Edit the file routes/web.php and include the following line

Route::get('products/emptycart', 'App\Http\Controllers\productController@emptycart')->name('products.emptycart');

Now to add the jquery code which will call this emptycart function as an ajax call - add the following code snippet inside the script tags at the bottom of the resources/views/products/displaygrid.blade.php file

$("#emptycart").click(function() { $.ajax({ 
    type: "get", url: "{{ url('products/emptycart')   }}",
    success: function() { 
        $('#shoppingcart').text(0); 
    }, 
    error: function() { 
        alert("problem communicating with the server");
    } 
  }); 
}); 

Leave a Reply