Come no ?!
Allora, qui recupero le informazioni dal modulo di acquisto
$code = $_POST['code'];
$qty = $_POST['qty'];
$item = $_POST['item'];
$price = $_POST['price'];
$sizing = ($_POST['sizing']) ? true : false;
$size = isset($_POST['size']) ? (string)$_POST['size'] : (int)0;
E qui creo o aggiorno l'array
$new = true;
if($qty == 0)
{
header("Location: shopping.php");
exit();
}
$subtotal = $qty * $price;
if(!empty($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $key => $value)
{
if($code === $value['code'] && ($size == $value['size']))
{
$_SESSION['cart'][$key]['qty'] += $qty;
$_SESSION['cart'][$key]['subtotal'] += $subtotal;
$new = false;
}
}
}
if($new == true)
{
$_SESSION['cart'][] = array(
'code' => $code,
'qty' => $qty,
'item' => $item,
'sizing' => $sizing,
'size' => $size,
'subtotal' => $subtotal);
}
Il secondo livello dell'array viene aggiornato solo se il codice del prodotto è presente, o se è presente ma la chiave size non coincide con l'input dell'utente.
Mi spiego: se ordino una T-Shirt taglia XL e nel mio carrello c'è una T-Shirt taglia Small, aggiungo una nuova array nel secondo livello. Se ho già una T-Shirt taglia Small nel carrello aggiorno solo la chiave quantità e il sub-totale del prezzo.