Solution for How to Add more than one products of same id with different sizes in laravel session?
is Given Below:
I am trying to add more than one product having same ID but different sizes. How can I do that. But It just override my previous product item that is added with the different size
if(!$cart) {
$cart = [
$request->product_id => [
"id" => $request->product_id,
"name" => $request->product_name,
"quantity" => $request->quantity,
"price" => $request->product_price,
"image" => $request->product_image,
"costprice" => $request->product_price,
"color" => $request->color,
"size" => $request->size,
]
];
session()->put('cart', $cart);
return redirect('/cart');
// return redirect()->back()->with('success', 'Product added to cart successfully!');
}
if(isset($cart[$request->product_id]) ) {
$cart[$request->product_id]['quantity']++;
session()->put('cart', $cart);
return redirect('/cart');
// return redirect()->back()->with('success', 'Product added to cart successfully!');
}
I think this code below will satisfy your requirement
if(!$cart)) {
$cart = array(
$request->product_id => array()
);
$productInfoArr=array(
"id"=> $request->product_id,
"name" => $request->product_name,
"quantity" => $request->quantity,
"price" => $request->product_price,
"image" => $request->product_image,
"costprice" => $request->product_price,
"color" => $request->color,
"size" => $request->size,
);
array_push($cart[$request->product_id],$productInfoArr);
session()->put('cart', $cart);
return redirect('/cart');
// return redirect()->back()->with('success', 'Product added to cart successfully!');
}
if(isset($cart[$request->product_id])) {
$productInfoArr=array(
"id"=> $request->product_id,
"name" => $request->product_name,
"quantity" => $request->quantity,
"price" => $request->product_price,
"image" => $request->product_image,
"costprice" => $request->product_price,
"color" => $request->color,
"size" => $request->size,
);
array_push($cart[$request->product_id],$productInfoArr);
session()->put('cart', $cart);
return redirect('/cart');
// return redirect()->back()->with('success', 'Product added to cart successfully!');
}