product.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. require_once __DIR__ . '/config.php';
  3. require_once __DIR__ . '/includes/functions.php';
  4. $productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  5. $product = getProductById($productId);
  6. if (!$product) {
  7. header('Location: index.php');
  8. exit;
  9. }
  10. $pageTitle = $product['name'];
  11. // Handle add to cart
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
  13. $quantity = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 1;
  14. $size = isset($_POST['size']) ? sanitize($_POST['size']) : '';
  15. // For apparel, size is required
  16. if ($product['category'] === 'apparel' && empty($size)) {
  17. $error = 'Bitte wählen Sie eine Größe aus.';
  18. } elseif ($quantity < 1) {
  19. $error = 'Menge muss mindestens 1 sein.';
  20. } else {
  21. // Add to session cart
  22. if (!isset($_SESSION['cart'])) {
  23. $_SESSION['cart'] = [];
  24. }
  25. // For apparel, check if same product with same size already in cart
  26. $found = false;
  27. if ($product['category'] === 'apparel') {
  28. foreach ($_SESSION['cart'] as &$item) {
  29. if ($item['product_id'] == $productId && isset($item['size']) && $item['size'] === $size) {
  30. $item['quantity'] += $quantity;
  31. $found = true;
  32. break;
  33. }
  34. }
  35. } else {
  36. foreach ($_SESSION['cart'] as &$item) {
  37. if ($item['product_id'] == $productId && !isset($item['size'])) {
  38. $item['quantity'] += $quantity;
  39. $found = true;
  40. break;
  41. }
  42. }
  43. }
  44. if (!$found) {
  45. $cartItem = [
  46. 'product_id' => $productId,
  47. 'quantity' => $quantity
  48. ];
  49. if ($product['category'] === 'apparel' && !empty($size)) {
  50. $cartItem['size'] = $size;
  51. }
  52. $_SESSION['cart'][] = $cartItem;
  53. }
  54. header('Location: cart.php');
  55. exit;
  56. }
  57. }
  58. include __DIR__ . '/includes/header.php';
  59. ?>
  60. <?php if (isset($error)): ?>
  61. <div class="alert alert-error">
  62. <?php echo htmlspecialchars($error); ?>
  63. </div>
  64. <?php endif; ?>
  65. <div class="product-detail-grid">
  66. <div>
  67. <?php if (!empty($product['image']) && file_exists(__DIR__ . '/assets/images/' . $product['image'])): ?>
  68. <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);">
  69. <?php else: ?>
  70. <div class="product-placeholder">
  71. Kein Bild verfügbar
  72. </div>
  73. <?php endif; ?>
  74. </div>
  75. <div>
  76. <h1><?php echo htmlspecialchars($product['name']); ?></h1>
  77. <div class="price" style="font-size: 2rem; margin: 1rem 0;"><?php echo formatPrice($product['price']); ?></div>
  78. <?php
  79. $totalStock = getTotalStock($product);
  80. $hasStock = $totalStock > 0;
  81. ?>
  82. <div class="stock <?php echo $hasStock ? 'in-stock' : 'out-of-stock'; ?>" style="font-size: 1.1rem; margin: 1rem 0;">
  83. <?php if ($hasStock): ?>
  84. <?php if ($product['category'] === 'apparel' && isset($product['stock_by_size']) && is_array($product['stock_by_size'])): ?>
  85. Verfügbar:
  86. <?php
  87. $sizes = is_array($product['sizes']) ? $product['sizes'] : explode(',', $product['sizes']);
  88. $stockInfo = [];
  89. foreach ($sizes as $sizeOption) {
  90. $sizeOption = trim($sizeOption);
  91. $sizeStock = isset($product['stock_by_size'][$sizeOption]) ? (int)$product['stock_by_size'][$sizeOption] : 0;
  92. if ($sizeStock > 0) {
  93. $stockInfo[] = "$sizeOption: $sizeStock";
  94. }
  95. }
  96. echo implode(', ', $stockInfo);
  97. if (empty($stockInfo)) {
  98. echo 'Ausverkauft';
  99. }
  100. ?>
  101. <?php else: ?>
  102. Verfügbar (<?php echo $totalStock; ?> Stück)
  103. <?php endif; ?>
  104. <?php else: ?>
  105. Ausverkauft - Vorbestellung möglich
  106. <?php endif; ?>
  107. </div>
  108. <div style="margin: 2rem 0;">
  109. <h3>Beschreibung</h3>
  110. <p style="margin-top: 0.5rem; line-height: 1.8;"><?php echo nl2br(htmlspecialchars($product['description'])); ?></p>
  111. </div>
  112. <form method="POST" style="margin-top: 2rem;">
  113. <?php if ($product['category'] === 'apparel' && !empty($product['sizes'])): ?>
  114. <div class="form-group">
  115. <label for="size">Größe *</label>
  116. <select id="size" name="size" required style="width: 100%;" onchange="updateMaxQuantity()">
  117. <option value="">Bitte wählen</option>
  118. <?php
  119. $sizes = is_array($product['sizes']) ? $product['sizes'] : explode(',', $product['sizes']);
  120. $stockBySize = isset($product['stock_by_size']) && is_array($product['stock_by_size']) ? $product['stock_by_size'] : [];
  121. foreach ($sizes as $sizeOption):
  122. $sizeOption = trim($sizeOption);
  123. $sizeStock = isset($stockBySize[$sizeOption]) ? (int)$stockBySize[$sizeOption] : 0;
  124. ?>
  125. <option value="<?php echo htmlspecialchars($sizeOption); ?>" data-stock="<?php echo $sizeStock; ?>">
  126. <?php echo htmlspecialchars($sizeOption); ?><?php echo $sizeStock <= 0 ? ' (Ausverkauft - Vorbestellung möglich)' : " ($sizeStock verfügbar)"; ?>
  127. </option>
  128. <?php endforeach; ?>
  129. </select>
  130. </div>
  131. <?php endif; ?>
  132. <div class="form-group">
  133. <label for="quantity">Menge:</label>
  134. <input type="number" id="quantity" name="quantity" value="1" min="1" max="<?php echo $hasStock ? $totalStock : 999; ?>" class="quantity-input" style="width: 100px;">
  135. </div>
  136. <?php if (!$hasStock): ?>
  137. <div class="alert alert-warning">
  138. <strong>Hinweis:</strong> Dieses Produkt wird vorbestellt. Lieferzeiten sind nicht bekannt.
  139. </div>
  140. <?php endif; ?>
  141. <button type="submit" name="add_to_cart" class="btn" style="width: 100%;">In den Warenkorb</button>
  142. </form>
  143. <?php if ($product['category'] === 'apparel' && !empty($product['sizes'])): ?>
  144. <script>
  145. function updateMaxQuantity() {
  146. const sizeSelect = document.getElementById('size');
  147. const quantityInput = document.getElementById('quantity');
  148. const selectedOption = sizeSelect.options[sizeSelect.selectedIndex];
  149. if (selectedOption && selectedOption.value) {
  150. const stock = parseInt(selectedOption.getAttribute('data-stock')) || 0;
  151. const max = stock > 0 ? stock : 999;
  152. quantityInput.max = max;
  153. if (parseInt(quantityInput.value) > max) {
  154. quantityInput.value = 1;
  155. }
  156. }
  157. }
  158. </script>
  159. <?php endif; ?>
  160. <div style="margin-top: 2rem;">
  161. <a href="index.php" class="btn btn-secondary">Zurück zur Übersicht</a>
  162. </div>
  163. </div>
  164. </div>
  165. <?php include __DIR__ . '/includes/footer.php'; ?>