product.php 7.3 KB

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