product.php 8.1 KB

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