cart.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. require_once __DIR__ . '/config.php';
  3. require_once __DIR__ . '/includes/functions.php';
  4. $pageTitle = 'Warenkorb';
  5. // Handle cart updates
  6. $currentCart = $_SESSION['cart'] ?? [];
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. if (isset($_POST['update_cart'])) {
  9. $quantities = $_POST['quantities'] ?? [];
  10. $newCart = [];
  11. foreach ($currentCart as $index => $item) {
  12. $newQty = isset($quantities[$index]) ? (int)$quantities[$index] : $item['quantity'];
  13. if ($newQty > 0) {
  14. $newItem = [
  15. 'product_id' => $item['product_id'],
  16. 'quantity' => $newQty
  17. ];
  18. if (isset($item['size']) && $item['size'] !== '') {
  19. $newItem['size'] = $item['size'];
  20. }
  21. $newCart[] = $newItem;
  22. }
  23. // If newQty <= 0, item is removed from cart
  24. }
  25. $_SESSION['cart'] = $newCart;
  26. } elseif (isset($_POST['remove_item_index'])) {
  27. $removeIndex = (int)$_POST['remove_item_index'];
  28. if (isset($currentCart[$removeIndex])) {
  29. unset($currentCart[$removeIndex]);
  30. $_SESSION['cart'] = array_values($currentCart); // Re-index
  31. }
  32. }
  33. }
  34. $cart = $_SESSION['cart'] ?? [];
  35. $cartItems = [];
  36. $total = 0;
  37. foreach ($cart as $index => $item) {
  38. $product = getProductById($item['product_id']);
  39. if ($product) {
  40. $itemTotal = $product['price'] * $item['quantity'];
  41. $total += $itemTotal;
  42. $cartItems[] = [
  43. 'product' => $product,
  44. 'quantity' => $item['quantity'],
  45. 'total' => $itemTotal,
  46. 'size' => isset($item['size']) ? $item['size'] : null,
  47. 'cart_index' => $index
  48. ];
  49. }
  50. }
  51. include __DIR__ . '/includes/header.php';
  52. ?>
  53. <h2>Warenkorb</h2>
  54. <?php if (empty($cartItems)): ?>
  55. <div class="alert alert-info">
  56. <p>Ihr Warenkorb ist leer.</p>
  57. <a href="index.php" class="btn">Weiter einkaufen</a>
  58. </div>
  59. <?php else: ?>
  60. <form method="POST">
  61. <?php foreach ($cartItems as $cartItem): ?>
  62. <div class="cart-item">
  63. <div class="cart-item-info">
  64. <h3><?php echo htmlspecialchars($cartItem['product']['name']); ?></h3>
  65. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  66. <p><strong>Größe:</strong> <?php echo htmlspecialchars($cartItem['size']); ?></p>
  67. <?php endif; ?>
  68. <p>Einzelpreis: <?php echo formatPrice($cartItem['product']['price']); ?></p>
  69. <p>Gesamt: <?php echo formatPrice($cartItem['total']); ?></p>
  70. <?php
  71. $itemStock = getStock($cartItem['product'], isset($cartItem['size']) ? $cartItem['size'] : null);
  72. $hasEnoughStock = $itemStock >= $cartItem['quantity'];
  73. ?>
  74. <p class="stock <?php echo $hasEnoughStock ? 'in-stock' : 'out-of-stock'; ?>">
  75. Lagerbestand: <?php echo $itemStock; ?> Stück
  76. <?php if (!$hasEnoughStock): ?>
  77. <br><strong class="status status-open">Vorbestellung möglich</strong>
  78. <?php endif; ?>
  79. </p>
  80. </div>
  81. <div class="cart-item-actions">
  82. <label>
  83. Menge:
  84. <input type="number" name="quantities[<?php echo $cartItem['cart_index']; ?>]"
  85. value="<?php echo $cartItem['quantity']; ?>"
  86. min="0"
  87. max="999"
  88. class="quantity-input">
  89. </label>
  90. <button type="submit" name="remove_item_index" value="<?php echo $cartItem['cart_index']; ?>" class="btn btn-secondary btn-small">Entfernen</button>
  91. </div>
  92. </div>
  93. <?php endforeach; ?>
  94. <div style="text-align: right; margin: 2rem 0;">
  95. <div style="font-size: 1.5rem; font-weight: bold; margin-bottom: 1rem;">
  96. Gesamtsumme: <?php echo formatPrice($total); ?>
  97. </div>
  98. <button type="submit" name="update_cart" class="btn btn-secondary">Warenkorb aktualisieren</button>
  99. <a href="checkout.php" class="btn" style="margin-left: 1rem;">Zur Reservierung</a>
  100. </div>
  101. </form>
  102. <?php endif; ?>
  103. <?php include __DIR__ . '/includes/footer.php'; ?>