You see, I'm trying to make my cart system for an online store. I want that when the user adds the same product again the quantity will change from 1 to 2 and so on.
The problem is that I can't make this quantity change. In the code that I leave, it is what I have tried, I was successful in checking if the product was already selected but it failed to update the quantity
<?php
//valores que recibo de los inputs
$id = $_GET['id_car'];
$nombre = $_GET['nombre'];
$foto = $_GET['foto'];
$precio = $_GET['precio'];
//cantidad por defecto es 1
$cantidad = 1;
// si no existe la session SHOP la creo y le asigno el primer producto
if (!isset($_SESSION['SHOP'])) {
$p_seleccionados = array(
'id' => $id,
'nombre' => $nombre,
'foto' => $foto,
'precio' => $precio,
'cantidad' => $cantidad
);
$_SESSION['SHOP'][0] = $p_seleccionados;
// aqui agrego el id de los productos para luego comprobar que no se dupliquen
$_SESSION['PRODUCTOS'][0] = $id;
} else {
$num_sel = count($_SESSION['SHOP']);
$coun_p = count($_SESSION['PRODUCTOS']);
//compruebo que no exista el id que acabo de seleccionar en array-session PRODUCTOS
if (!in_array($id, $_SESSION['PRODUCTOS'])) {
$_SESSION['PRODUCTOS'][$coun_p] = $productos;
$p_seleccionados = array(
'id' => $id,
'nombre' => $nombre,
'foto' => $foto,
'precio' => $precio,
'cantidad' => $cantidad
);
$_SESSION['SHOP'][$num_sel] = $p_seleccionados;
$_SESSION['PRODUCTOS'][$coun_p] = $id;
} else {
// esto se ejecuta si el usuario ha seleccionado un producto que ya selecciono anteriormente el problema es que la cantidad no se actualiza
foreach ($_SESSION['SHOP'] as $indice=>$producto) {
if ($producto['id'] == $id) {
$cantidad =+ 1;
$producto['cantidad'] = $cantidad;
}
}
}
print_r($_SESSION['SHOP']);
}
?>