I'm trying to create add to cart function, and the product is stored inside an array. The array is like this:
array:2 [▼
0 => array:2 [▼
"product_id" => "Produk-0036"
"quantity" => "2"
]
1 => array:2 [▼
"product_id" => "Produk-0037"
"quantity" => "3"
]
]
My question is how to update the quantity if product_id already exist inside the array?
$get_cart = Cookie::get('user_cart');
if(isset($get_cart)) {
$cart_array[] = json_decode($get_cart);
}
$cart_array[] = ["product_id" => $request->product_id, "quantity" => $request->quantity];
$cart_json = json_encode($cart_array);
$cart_cookies = Cookie::forever('user_cart', $cart_json);
Using the code above will add new index in the array instead of updating the quantity of already exist product
Thanks