| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- require_once __DIR__ . '/config.php';
- 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 ($currentCart as $index => $item) {
- $newQty = isset($quantities[$index]) ? (int)$quantities[$index] : $item['quantity'];
- if ($newQty > 0) {
- $newItem = [
- 'product_id' => $item['product_id'],
- 'quantity' => $newQty
- ];
- if (isset($item['size']) && $item['size'] !== '') {
- $newItem['size'] = $item['size'];
- }
- $newCart[] = $newItem;
- }
- // If newQty <= 0, item is removed from cart
- }
- $_SESSION['cart'] = $newCart;
- } 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
- }
- }
- }
- $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 class="status status-open">Vorbestellung möglich</strong>
- <?php endif; ?>
- </p>
- </div>
- <div class="cart-item-actions">
- <label>
- Menge:
- <input type="number" name="quantities[<?php echo $cartItem['cart_index']; ?>]"
- value="<?php echo $cartItem['quantity']; ?>"
- min="0"
- max="999"
- class="quantity-input">
- </label>
- <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; ?>
-
- <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'; ?>
|