cart.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. require_once __DIR__ . '/config.php';
  3. require_once __DIR__ . '/includes/functions.php';
  4. $pageTitle = 'Warenkorb';
  5. // Handle cart updates
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7. if (isset($_POST['update_cart'])) {
  8. $quantities = $_POST['quantities'] ?? [];
  9. foreach ($quantities as $productId => $quantity) {
  10. $quantity = (int)$quantity;
  11. if ($quantity <= 0) {
  12. // Remove from cart
  13. $_SESSION['cart'] = array_filter($_SESSION['cart'], function($item) use ($productId) {
  14. return $item['product_id'] != $productId;
  15. });
  16. $_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index
  17. } else {
  18. // Update quantity
  19. foreach ($_SESSION['cart'] as &$item) {
  20. if ($item['product_id'] == $productId) {
  21. $item['quantity'] = $quantity;
  22. break;
  23. }
  24. }
  25. }
  26. }
  27. } elseif (isset($_POST['remove_item'])) {
  28. $productId = (int)$_POST['product_id'];
  29. $size = isset($_POST['size']) ? sanitize($_POST['size']) : null;
  30. $_SESSION['cart'] = array_filter($_SESSION['cart'], function($item) use ($productId, $size) {
  31. if ($item['product_id'] != $productId) {
  32. return true;
  33. }
  34. // If size is specified, only remove items with matching size
  35. if ($size !== null) {
  36. return !(isset($item['size']) && $item['size'] === $size);
  37. }
  38. // If no size specified, remove all items of this product
  39. return false;
  40. });
  41. $_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index
  42. }
  43. }
  44. $cart = $_SESSION['cart'] ?? [];
  45. $cartItems = [];
  46. $total = 0;
  47. foreach ($cart as $index => $item) {
  48. $product = getProductById($item['product_id']);
  49. if ($product) {
  50. $itemTotal = $product['price'] * $item['quantity'];
  51. $total += $itemTotal;
  52. $cartItems[] = [
  53. 'product' => $product,
  54. 'quantity' => $item['quantity'],
  55. 'total' => $itemTotal,
  56. 'size' => isset($item['size']) ? $item['size'] : null,
  57. 'cart_index' => $index
  58. ];
  59. }
  60. }
  61. include __DIR__ . '/includes/header.php';
  62. ?>
  63. <h2>Warenkorb</h2>
  64. <?php if (empty($cartItems)): ?>
  65. <div class="alert alert-info">
  66. <p>Ihr Warenkorb ist leer.</p>
  67. <a href="index.php" class="btn">Weiter einkaufen</a>
  68. </div>
  69. <?php else: ?>
  70. <form method="POST">
  71. <?php foreach ($cartItems as $cartItem): ?>
  72. <div class="cart-item">
  73. <div class="cart-item-info">
  74. <h3><?php echo htmlspecialchars($cartItem['product']['name']); ?></h3>
  75. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  76. <p><strong>Größe:</strong> <?php echo htmlspecialchars($cartItem['size']); ?></p>
  77. <?php endif; ?>
  78. <p>Einzelpreis: <?php echo formatPrice($cartItem['product']['price']); ?></p>
  79. <p>Gesamt: <?php echo formatPrice($cartItem['total']); ?></p>
  80. <?php
  81. $itemStock = getStock($cartItem['product'], isset($cartItem['size']) ? $cartItem['size'] : null);
  82. $hasEnoughStock = $itemStock >= $cartItem['quantity'];
  83. ?>
  84. <p class="stock <?php echo $hasEnoughStock ? 'in-stock' : 'out-of-stock'; ?>">
  85. Lagerbestand: <?php echo $itemStock; ?> Stück
  86. <?php if (!$hasEnoughStock): ?>
  87. <br><strong style="color: #dc3545;">Nicht genügend Lagerbestand!</strong>
  88. <?php endif; ?>
  89. </p>
  90. </div>
  91. <div class="cart-item-actions">
  92. <label>
  93. Menge:
  94. <input type="number" name="quantities[<?php echo $cartItem['product']['id']; ?>]"
  95. value="<?php echo $cartItem['quantity']; ?>"
  96. min="0"
  97. max="<?php echo $itemStock; ?>"
  98. class="quantity-input">
  99. </label>
  100. <form method="POST" style="display: inline;">
  101. <input type="hidden" name="product_id" value="<?php echo $cartItem['product']['id']; ?>">
  102. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  103. <input type="hidden" name="size" value="<?php echo htmlspecialchars($cartItem['size']); ?>">
  104. <?php endif; ?>
  105. <button type="submit" name="remove_item" value="1" class="btn btn-secondary btn-small">Entfernen</button>
  106. </form>
  107. </div>
  108. </div>
  109. <?php endforeach; ?>
  110. <div style="text-align: right; margin: 2rem 0;">
  111. <div style="font-size: 1.5rem; font-weight: bold; margin-bottom: 1rem;">
  112. Gesamtsumme: <?php echo formatPrice($total); ?>
  113. </div>
  114. <button type="submit" name="update_cart" class="btn btn-secondary">Warenkorb aktualisieren</button>
  115. <a href="checkout.php" class="btn" style="margin-left: 1rem;">Zur Reservierung</a>
  116. </div>
  117. </form>
  118. <?php endif; ?>
  119. <?php include __DIR__ . '/includes/footer.php'; ?>