Selaa lähdekoodia

fixing cart update functionality

Medowar 1 kuukausi sitten
vanhempi
sitoutus
f5401622f9
1 muutettua tiedostoa jossa 10 lisäystä ja 24 poistoa
  1. 10 24
      cart.php

+ 10 - 24
cart.php

@@ -5,12 +5,14 @@ require_once __DIR__ . '/includes/functions.php';
 $pageTitle = 'Warenkorb';
 
 // Handle cart updates
+$currentCart = $_SESSION['cart'] ?? [];
+
 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     if (isset($_POST['update_cart'])) {
         $quantities = $_POST['quantities'] ?? [];
         $newCart = [];
 
-        foreach ($_SESSION['cart'] as $index => $item) {
+        foreach ($currentCart as $index => $item) {
             $newQty = isset($quantities[$index]) ? (int)$quantities[$index] : $item['quantity'];
 
             if ($newQty > 0) {
@@ -27,22 +29,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         }
 
         $_SESSION['cart'] = $newCart;
-    } elseif (isset($_POST['remove_item'])) {
-        $productId = (int)$_POST['product_id'];
-        $size = isset($_POST['size']) ? sanitize($_POST['size']) : null;
-        
-        $_SESSION['cart'] = array_filter($_SESSION['cart'], function($item) use ($productId, $size) {
-            if ($item['product_id'] != $productId) {
-                return true;
-            }
-            // If size is specified, only remove items with matching size
-            if ($size !== null) {
-                return !(isset($item['size']) && $item['size'] === $size);
-            }
-            // If no size specified, remove all items of this product
-            return false;
-        });
-        $_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index
+    } elseif (isset($_POST['remove_item_index'])) {
+        $removeIndex = (int)$_POST['remove_item_index'];
+        if (isset($currentCart[$removeIndex])) {
+            unset($currentCart[$removeIndex]);
+            $_SESSION['cart'] = array_values($currentCart); // Re-index
+        }
     }
 }
 
@@ -106,13 +98,7 @@ include __DIR__ . '/includes/header.php';
                                max="999"
                                class="quantity-input">
                     </label>
-                    <form method="POST" style="display: inline;">
-                        <input type="hidden" name="product_id" value="<?php echo $cartItem['product']['id']; ?>">
-                        <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
-                            <input type="hidden" name="size" value="<?php echo htmlspecialchars($cartItem['size']); ?>">
-                        <?php endif; ?>
-                        <button type="submit" name="remove_item" value="1" class="btn btn-secondary btn-small">Entfernen</button>
-                    </form>
+                    <button type="submit" name="remove_item_index" value="<?php echo $cartItem['cart_index']; ?>" class="btn btn-secondary btn-small">Entfernen</button>
                 </div>
             </div>
         <?php endforeach; ?>