$(document).ready(function () { let panier = {}; function afficherPanier() { const $tbody = $('#panier-items'); $tbody.empty(); let totalGlobal = 0; $.each(panier, function (id, item) { const total = item.prix * item.quantite; totalGlobal += total; $tbody.append(` ${item.nom} ${item.prix}€ ${item.quantite} ${total}€ `); }); $('#panier-count').text(`Panier (${Object.keys(panier).length})`); if (totalGlobal === 0) { $tbody.append('Panier vide'); } } $('.ajouter-panier').click(function () { const produit = $(this).closest('.produit'); const id = produit.data('id'); const nom = produit.data('nom'); const prix = produit.data('prix'); if (panier[id]) { panier[id].quantite += 1; } else { panier[id] = { nom, prix, quantite: 1 }; } afficherPanier(); }); $('#panier-items').on('click', '.retirer', function () { const id = $(this).data('id'); delete panier[id]; afficherPanier(); }); });