| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- require_once __DIR__ . '/config.php';
- require_once __DIR__ . '/includes/functions.php';
- $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
- $product = getProductById($productId);
- if (!$product) {
- header('Location: index.php');
- exit;
- }
- $pageTitle = $product['name'];
- // Handle add to cart
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
- $quantity = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 1;
- $size = isset($_POST['size']) ? sanitize($_POST['size']) : '';
-
- // For apparel, size is required
- if ($product['category'] === 'apparel' && empty($size)) {
- $error = 'Bitte wählen Sie eine Größe aus.';
- } elseif ($quantity < 1) {
- $error = 'Menge muss mindestens 1 sein.';
- } else {
- // Add to session cart
- if (!isset($_SESSION['cart'])) {
- $_SESSION['cart'] = [];
- }
-
- // For apparel, check if same product with same size already in cart
- $found = false;
- if ($product['category'] === 'apparel') {
- foreach ($_SESSION['cart'] as &$item) {
- if ($item['product_id'] == $productId && isset($item['size']) && $item['size'] === $size) {
- $item['quantity'] += $quantity;
- $found = true;
- break;
- }
- }
- } else {
- foreach ($_SESSION['cart'] as &$item) {
- if ($item['product_id'] == $productId && !isset($item['size'])) {
- $item['quantity'] += $quantity;
- $found = true;
- break;
- }
- }
- }
-
- if (!$found) {
- $cartItem = [
- 'product_id' => $productId,
- 'quantity' => $quantity
- ];
- if ($product['category'] === 'apparel' && !empty($size)) {
- $cartItem['size'] = $size;
- }
- $_SESSION['cart'][] = $cartItem;
- }
-
- header('Location: cart.php');
- exit;
- }
- }
- include __DIR__ . '/includes/header.php';
- ?>
- <?php if (isset($error)): ?>
- <div class="alert alert-error">
- <?php echo htmlspecialchars($error); ?>
- </div>
- <?php endif; ?>
- <div class="product-detail-grid">
- <div>
- <?php if (!empty($product['image']) && file_exists(__DIR__ . '/assets/images/' . $product['image'])): ?>
- <img src="<?php echo SITE_URL; ?>/assets/images/<?php echo htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" style="width: 100%; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
- <?php else: ?>
- <div class="product-placeholder">
- Kein Bild verfügbar
- </div>
- <?php endif; ?>
- </div>
-
- <div>
- <h1><?php echo htmlspecialchars($product['name']); ?></h1>
- <div class="price" style="font-size: 2rem; margin: 1rem 0;"><?php echo formatPrice($product['price']); ?></div>
-
- <?php
- $totalStock = getTotalStock($product);
- $hasStock = $totalStock > 0;
- ?>
- <div class="stock <?php echo $hasStock ? 'in-stock' : 'out-of-stock'; ?>" style="font-size: 1.1rem; margin: 1rem 0;">
- <?php if ($hasStock): ?>
- <?php if ($product['category'] === 'apparel' && isset($product['stock_by_size']) && is_array($product['stock_by_size'])): ?>
- Verfügbar:
- <?php
- $sizes = is_array($product['sizes']) ? $product['sizes'] : explode(',', $product['sizes']);
- $stockInfo = [];
- foreach ($sizes as $sizeOption) {
- $sizeOption = trim($sizeOption);
- $sizeStock = isset($product['stock_by_size'][$sizeOption]) ? (int)$product['stock_by_size'][$sizeOption] : 0;
- if ($sizeStock > 0) {
- $stockInfo[] = "$sizeOption: $sizeStock";
- }
- }
- echo implode(', ', $stockInfo);
- if (empty($stockInfo)) {
- echo 'Ausverkauft';
- }
- ?>
- <?php else: ?>
- Verfügbar (<?php echo $totalStock; ?> Stück)
- <?php endif; ?>
- <?php else: ?>
- Ausverkauft - Vorbestellung möglich
- <?php endif; ?>
- </div>
-
- <div style="margin: 2rem 0;">
- <h3>Beschreibung</h3>
- <p style="margin-top: 0.5rem; line-height: 1.8;"><?php echo nl2br(htmlspecialchars($product['description'])); ?></p>
- </div>
-
- <form method="POST" style="margin-top: 2rem;">
- <?php if ($product['category'] === 'apparel' && !empty($product['sizes'])): ?>
- <div class="form-group">
- <label for="size">Größe *</label>
- <select id="size" name="size" required style="width: 100%;" onchange="updateMaxQuantity()">
- <option value="">Bitte wählen</option>
- <?php
- $sizes = is_array($product['sizes']) ? $product['sizes'] : explode(',', $product['sizes']);
- $stockBySize = isset($product['stock_by_size']) && is_array($product['stock_by_size']) ? $product['stock_by_size'] : [];
- foreach ($sizes as $sizeOption):
- $sizeOption = trim($sizeOption);
- $sizeStock = isset($stockBySize[$sizeOption]) ? (int)$stockBySize[$sizeOption] : 0;
- ?>
- <option value="<?php echo htmlspecialchars($sizeOption); ?>" data-stock="<?php echo $sizeStock; ?>">
- <?php echo htmlspecialchars($sizeOption); ?><?php echo $sizeStock <= 0 ? ' (Ausverkauft - Vorbestellung möglich)' : " ($sizeStock verfügbar)"; ?>
- </option>
- <?php endforeach; ?>
- </select>
- </div>
- <?php endif; ?>
- <div class="form-group">
- <label for="quantity">Menge:</label>
- <input type="number" id="quantity" name="quantity" value="1" min="1" max="<?php echo $hasStock ? $totalStock : 999; ?>" class="quantity-input" style="width: 100px;">
- </div>
- <?php if (!$hasStock): ?>
- <div class="alert alert-warning">
- <strong>Hinweis:</strong> Dieses Produkt wird vorbestellt. Lieferzeiten sind nicht bekannt.
- </div>
- <?php endif; ?>
- <button type="submit" name="add_to_cart" class="btn" style="width: 100%;">In den Warenkorb</button>
- </form>
- <?php if ($product['category'] === 'apparel' && !empty($product['sizes'])): ?>
- <script>
- function updateMaxQuantity() {
- const sizeSelect = document.getElementById('size');
- const quantityInput = document.getElementById('quantity');
- const selectedOption = sizeSelect.options[sizeSelect.selectedIndex];
- if (selectedOption && selectedOption.value) {
- const stock = parseInt(selectedOption.getAttribute('data-stock')) || 0;
- const max = stock > 0 ? stock : 999;
- quantityInput.max = max;
- if (parseInt(quantityInput.value) > max) {
- quantityInput.value = 1;
- }
- }
- }
- </script>
- <?php endif; ?>
-
- <div style="margin-top: 2rem;">
- <a href="index.php" class="btn btn-secondary">Zurück zur Übersicht</a>
- </div>
- </div>
- </div>
- <?php include __DIR__ . '/includes/footer.php'; ?>
|