getConnection();
// Inizializza il carrello se non esiste
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
// Recupera i prodotti nel carrello
$cart_items = [];
$total_price = 0;
if (!empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $cart_key => $item) {
$product_id = $item['product_id'];
$variation_id = $item['variation_id'];
$class_type_id = $item['class_type_id'];
$quantity = $item['quantity'];
// Query per ottenere i dettagli del prodotto, variazione e classe
$stmt = $pdo->prepare("
SELECT p.name AS product_name, pv.name AS variation_name, pv.price, c.name AS class_name, c.photo AS class_photo,
ct.level, ct.day_of_week
FROM products p
JOIN product_variations pv ON pv.id = ?
LEFT JOIN product_class_types pct ON p.id = pct.product_id AND pct.variation_id IS NULL
LEFT JOIN class_types ct ON ct.id = ?
LEFT JOIN classes c ON ct.class_id = c.id
WHERE p.id = ?
");
$stmt->execute([$variation_id, $class_type_id, $product_id]);
$cart_item = $stmt->fetch(PDO::FETCH_ASSOC);
if ($cart_item) {
$cart_items[$cart_key] = [
'product_name' => $cart_item['product_name'],
'variation_name' => $cart_item['variation_name'],
'class_name' => $cart_item['class_name'],
'level' => $cart_item['level'],
'day_of_week' => $cart_item['day_of_week'],
'photo' => $cart_item['class_photo'] ?: 'default_class_image.jpg',
'price' => $cart_item['price'],
'quantity' => $quantity,
'subtotal' => $cart_item['price'] * $quantity
];
$total_price += $cart_item['price'] * $quantity;
}
}
}
// Calcola il numero totale di elementi nel carrello
$cart_count = array_sum(array_column($_SESSION['cart'], 'quantity'));
?>