| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- require_once __DIR__ . '/config.php';
- require_once __DIR__ . '/includes/functions.php';
- $pageTitle = 'Warenkorb';
- // Handle cart updates
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if (isset($_POST['update_cart'])) {
- $quantities = $_POST['quantities'] ?? [];
- foreach ($quantities as $productId => $quantity) {
- $quantity = (int)$quantity;
- if ($quantity <= 0) {
- // Remove from cart
- $_SESSION['cart'] = array_filter($_SESSION['cart'], function($item) use ($productId) {
- return $item['product_id'] != $productId;
- });
- $_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index
- } else {
- // Update quantity
- foreach ($_SESSION['cart'] as &$item) {
- if ($item['product_id'] == $productId) {
- $item['quantity'] = $quantity;
- break;
- }
- }
- }
- }
- } 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
- }
- }
- $cart = $_SESSION['cart'] ?? [];
- $cartItems = [];
- $total = 0;
- foreach ($cart as $index => $item) {
- $product = getProductById($item['product_id']);
- if ($product) {
- $itemTotal = $product['price'] * $item['quantity'];
- $total += $itemTotal;
- $cartItems[] = [
- 'product' => $product,
- 'quantity' => $item['quantity'],
- 'total' => $itemTotal,
- 'size' => isset($item['size']) ? $item['size'] : null,
- 'cart_index' => $index
- ];
- }
- }
- include __DIR__ . '/includes/header.php';
- ?>
- <h2>Warenkorb</h2>
- <?php if (empty($cartItems)): ?>
- <div class="alert alert-info">
- <p>Ihr Warenkorb ist leer.</p>
- <a href="index.php" class="btn">Weiter einkaufen</a>
- </div>
- <?php else: ?>
- <form method="POST">
- <?php foreach ($cartItems as $cartItem): ?>
- <div class="cart-item">
- <div class="cart-item-info">
- <h3><?php echo htmlspecialchars($cartItem['product']['name']); ?></h3>
- <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
- <p><strong>Größe:</strong> <?php echo htmlspecialchars($cartItem['size']); ?></p>
- <?php endif; ?>
- <p>Einzelpreis: <?php echo formatPrice($cartItem['product']['price']); ?></p>
- <p>Gesamt: <?php echo formatPrice($cartItem['total']); ?></p>
- <?php
- $itemStock = getStock($cartItem['product'], isset($cartItem['size']) ? $cartItem['size'] : null);
- $hasEnoughStock = $itemStock >= $cartItem['quantity'];
- ?>
- <p class="stock <?php echo $hasEnoughStock ? 'in-stock' : 'out-of-stock'; ?>">
- Lagerbestand: <?php echo $itemStock; ?> Stück
- <?php if (!$hasEnoughStock): ?>
- <br><strong style="color: #dc3545;">Nicht genügend Lagerbestand!</strong>
- <?php endif; ?>
- </p>
- </div>
- <div class="cart-item-actions">
- <label>
- Menge:
- <input type="number" name="quantities[<?php echo $cartItem['product']['id']; ?>]"
- value="<?php echo $cartItem['quantity']; ?>"
- min="0"
- max="<?php echo $itemStock; ?>"
- 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>
- </div>
- </div>
- <?php endforeach; ?>
-
- <div style="text-align: right; margin: 2rem 0;">
- <div style="font-size: 1.5rem; font-weight: bold; margin-bottom: 1rem;">
- Gesamtsumme: <?php echo formatPrice($total); ?>
- </div>
- <button type="submit" name="update_cart" class="btn btn-secondary">Warenkorb aktualisieren</button>
- <a href="checkout.php" class="btn" style="margin-left: 1rem;">Zur Reservierung</a>
- </div>
- </form>
- <?php endif; ?>
- <?php include __DIR__ . '/includes/footer.php'; ?>
|