In this video we are going to learn about Shopping Cart.So let see how can we create shopping cart.
For creating shopping cart we use a shoppingcart package
So first of all lets install shopping cart package
For that switch to the command prompt and for installing shopping cart package just type here the command.

composer require hardevine/shoppingcart

Now switch to the project
Lets configure this package
For that just open config\\app.php
And inside the providers array add this

Gloudemans\\Shoppingcart\\ShoppingcartServiceProvider::class, 

Now inside the aliases array add  

'Cart' => Gloudemans\\Shoppingcart\\Facades\\Cart::class,

Now lets publish the configuration

php artisan vendor:publish --provider=\"Gloudemans\\Shoppingcart\\ShoppingcartServiceProvider\" --tag=\"config\"

Now open ShopComponent class file
And lets create a function for storing product into the cart

public function store($product_id,$product_name,$product_price)
{
Cart::instance('cart')->add($product_id,$product_name,1,$product_price)->associate('App\\Models\\Product');
session()->flash('success_message','Item added in Cart');
return redirect()->route('product.cart');
}


Now lets open shop-component.blade.php view file

<a href=\"#\" class=\"btn add-to-cart\" wire:click.prevent=\"store({{$product->id}},'{{$product->name}}',{{$product->regular_price}})\">Add To Cart</a>

Now open cart-component.blade.php view file and lets make things dynamic on this cart page. Inside the div.wrap-iten-in-cart

@if(Cart::instance('cart')->count() > 0)
<div class=\"wrap-iten-in-cart\">
@if(Session::has('success_message'))
<div class=\"alert alert-success\">
<strong>Success</strong> {{Session::get('success_message')}}
</div>
@endif
@if(Cart::instance('cart')->count() > 0)
<h3 class=\"box-title\">Products Name</h3>
<ul class=\"products-cart\">
@foreach (Cart::instance('cart')->content() as $item)
<li class=\"pr-cart-item\">
<div class=\"product-image\">
<figure><img src=\"{{ ('assets/images/products') }}/{{$item->model->image}}\" alt=\"{{$item->model->name}}\"></figure>
</div>
<div class=\"product-name\">
<a class=\"link-to-product\" href=\"{{route('product.details',['slug'=>$item->model->slug])}}\">{{$item->model->name}}</a>
</div>
<div class=\"price-field produtc-price\"><p class=\"price\">${{$item->model->regular_price}}</p></div>
<div class=\"quantity\">
<div class=\"quantity-input\">
<input type=\"text\" name=\"product-quatity\" value=\"{{$item->qty}}\" data-max=\"120\" pattern=\"[0-9]*\" >
<a class=\"btn btn-increase\" href=\"#\"></a>
<a class=\"btn btn-reduce\" href=\"#\"></a>
</div>
<p class=\"text-center\"><a href=\"#\">Save For Later</a></p>
</div>
<div class=\"price-field sub-total\"><p class=\"price\">${{$item->subtotal}}</p></div>

<div class=\"delete\">
<a href=\"#\" class=\"btn btn-delete\" title=\"\">
<span>Delete from your cart</span>
<i class=\"fa fa-times-circle\" aria-hidden=\"true\"></i>
</a>
</div>
</li>
@endforeach
</ul>
@else
<p>No item in cart</p>
@endif
</div>


Now inside the div.order-summary write following code.

<div class=\"order-summary\">
<h4 class=\"title-box\">Order Summary</h4>
<p class=\"summary-info\"><span class=\"title\">Subtotal</span><b class=\"index\">${{Cart::instance('cart')->subtotal()}}</b></p>
@if(Session::has('coupon'))
<p class=\"summary-info\"><span class=\"title\">Discount ({{Session::get('coupon')['code']}}) <a href=\"#\" wire:click.prevent= \"removeCoupon\"><i class=\"fa fa-times text-danger\"></i></a></span></span><b class=\"index\"> -${{number_format($discount,2)}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Subtotal with Discount</span></span><b class=\"index\">${{number_format($subtotalAfterDiscount,2)}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Tax ({{config('cart.tax')}}%)</span></span><b class=\"index\">${{number_format($taxAfterDiscount,2)}}</b></p>
<p class=\"summary-info total-info \"><span class=\"title\">Total</span><b class=\"index\">${{number_format($totalAfterDiscount,2)}}</b></p>
@else
<p class=\"summary-info\"><span class=\"title\">Tax</span><b class=\"index\">${{Cart::instance('cart')->tax()}}</b></p>
<p class=\"summary-info\"><span class=\"title\">Shipping</span><b class=\"index\">Free Shipping</b></p>
<p class=\"summary-info total-info \"><span class=\"title\">Total</span><b class=\"index\">${{Cart::instance('cart')->total()}}</b></p>
@endif
</div>



And now inside the order summary You can configure tax rate from cart configuration file
For getting the cart configuration file go to the app->config->cart.php
And here you can see the default tax rate is 21 percent
You can also configure the number format
Now lets display the success message inside the cart component view file
For that just write here Now open DetailsComponent.php class file and create a function

public function store($product_id,$product_name,$product_price)
{
Cart::instance('cart')->add($product_id,$product_name,$this->qty,$product_price)->associate('App\\Models\\Product');
session()->flash('success_message','Item added in Cart');
return redirect()->route('product.cart');
}


Now open details-component.blade.php view file and here just call the function from add to cart button

<a href=\"#\" class=\"btn add-to-cart\" wire:click.prevent=\"store({{$product->id}},'{{$product->name}}',{{$product->regular_price}})\">Add to Cart</a>


Now its done lets check it. So switch to the browser and just go the shop page and now add any product to the cart. And on Cart page you can see here product has been added.
Now lets add product into cart from details page. So first click one any product , here is the product details page. Now click on add to cart. You can see here product has been added.
Now lets show the no of product in cart on header. For that just open base.blade.php layout file.
And inside this file lets add the following code

<div class=\"wrap-icon-section minicart\">
<a href=\"{{route('product.cart')}}\" class=\"link-direction\">
<i class=\"fa fa-shopping-basket\" aria-hidden=\"true\"></i>
<div class=\"left-info\">
@if(Cart::instance('cart')->count() > 0)
<span class=\"index\">{{Cart::instance('cart')->count()}} items</span>
@endif
<span class=\"title\">CART</span>
</div>
</a>
</div>


Now its done so lets check
So switch to the browser and just refresh the page
And here you can see the no of products in to the cart
Now lets add new product in cart. You can see the no of product.
So in this way you can create the shopping cart in laravel 8 ecommerce.