I have this function which allows user to add product to cart but if the product is deleted in admin panel it still shows in the cart, any idea on how to remove the product in cart which has been deleted in admin dashboard?
Controller
public function addToCart($id)
{
$userId = Auth::id();
$products = Product::with('images')->find($id);
if(!$products) {
abort(404);
}
$cart = session()->get('cart');
// if cart is empty then this is the first product
if(!$cart) {
$cart = [
$id => [
"name" => $products->pro_name,
"quantity" => 1,
"price" => $products->pro_price,
]
];
session()->put('cart', $cart);
return redirect()->back()
}
// if cart is not empty then check if this product exist then increment quantity
if(isset($cart[$id])) {
$cart[$id]['quantity']++;
session()->put('cart', $cart);
return redirect()->back()
}
// if item not exist in cart then add to cart with quantity = 1
$cart[$id] = [
"name" => $products->pro_name,
"quantity" => 1,
"price" => $products->pro_price,
];
session()->put('cart', $cart);
return redirect()->back()
}
Blade File
@if(session('cart'))
@foreach(session('cart') as $id => $details)
<h5 class="px" ></h5>
<h4 class="ft" ></h4>
@endforeach
@endif
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire