41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// Inizializza il carrello se non esiste
|
|
if (!isset($_SESSION['cart'])) {
|
|
$_SESSION['cart'] = [];
|
|
}
|
|
|
|
// Recupera i dati dal POST
|
|
$product_id = $_POST['product_id'] ?? 0;
|
|
$variation_id = $_POST['variation_id'] ?? 0;
|
|
$class_type_id = $_POST['class_type_id'] ?? 0;
|
|
$quantity = $_POST['quantity'] ?? 1;
|
|
|
|
// Validazione
|
|
if ($product_id <= 0 || $variation_id <= 0 || $class_type_id <= 0) {
|
|
echo json_encode(['success' => false, 'message' => 'Dati non validi']);
|
|
exit;
|
|
}
|
|
|
|
// Chiave univoca per l'elemento nel carrello
|
|
$cart_key = $product_id . '-' . $variation_id . '-' . $class_type_id;
|
|
|
|
// Aggiungi o aggiorna il prodotto nel carrello
|
|
if (isset($_SESSION['cart'][$cart_key])) {
|
|
$_SESSION['cart'][$cart_key]['quantity'] += $quantity;
|
|
} else {
|
|
$_SESSION['cart'][$cart_key] = [
|
|
'product_id' => $product_id,
|
|
'variation_id' => $variation_id,
|
|
'class_type_id' => $class_type_id,
|
|
'quantity' => $quantity
|
|
];
|
|
}
|
|
|
|
// Calcola il numero totale di elementi nel carrello
|
|
$cart_count = array_sum(array_column($_SESSION['cart'], 'quantity'));
|
|
|
|
// Rispondi con successo
|
|
echo json_encode(['success' => true, 'cart_count' => $cart_count]);
|