$item) { if ($item['id'] == $id_produit) { $_SESSION['panier'][$key]['quantite'] = $quantite; break; } } } // Supprimer un produit if ($_POST['action'] === 'remove') { foreach ($_SESSION['panier'] as $key => $item) { if ($item['id'] == $id_produit) { unset($_SESSION['panier'][$key]); break; } } // Réindexer le tableau $_SESSION['panier'] = array_values($_SESSION['panier']); } // Rediriger pour éviter la resoumission du formulaire header("Location: panier.php"); exit(); } // Mettre à jour les informations des produits dans le panier à partir de la base de données if (!empty($_SESSION['panier'])) { foreach ($_SESSION['panier'] as $key => $item) { $id_produit = $item['id']; // Requête pour obtenir les informations à jour du produit $stmt = $conn->prepare("SELECT id, nom, description, prix, image, categorie, stock FROM produits WHERE id = ?"); $stmt->bind_param("i", $id_produit); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $produit = $result->fetch_assoc(); // Mettre à jour les informations du produit dans le panier $_SESSION['panier'][$key]['nom'] = $produit['nom']; $_SESSION['panier'][$key]['description'] = $produit['description']; $_SESSION['panier'][$key]['prix'] = $produit['prix']; $_SESSION['panier'][$key]['image'] = $produit['image']; $_SESSION['panier'][$key]['stock'] = $produit['stock']; // Vérifier que la quantité ne dépasse pas le stock disponible if ($_SESSION['panier'][$key]['quantite'] > $produit['stock']) { $_SESSION['panier'][$key]['quantite'] = $produit['stock']; } // Si le stock est à 0, supprimer l'article du panier if ($produit['stock'] <= 0) { unset($_SESSION['panier'][$key]); } } else { // Si le produit n'existe plus en base de données, le retirer du panier unset($_SESSION['panier'][$key]); } } // Réindexer le tableau après suppression éventuelle $_SESSION['panier'] = array_values($_SESSION['panier']); } // Calculer les totaux $sous_total = 0; $taxes = 0; $total = 0; foreach ($_SESSION['panier'] as $item) { $sous_total += $item['prix'] * $item['quantite']; } // Calculer taxes si nécessaire (0% dans l'exemple) $taxes = 0; $total = $sous_total + $taxes; // Fonction pour vérifier si une image existe function image_exists($image_path) { return file_exists($image_path); } ?>
Votre panier est vide.