cart.php 5.3 KB

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