product.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 === null) {
  7. header('Location: index.php');
  8. exit;
  9. }
  10. $pageTitle = $product['name'];
  11. $sizes = getProductSizes($product);
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
  13. $size = trim((string) ($_POST['size'] ?? ''));
  14. if (!empty($sizes) && ($size === '' || !in_array($size, $sizes, true))) {
  15. $error = 'Bitte wählen Sie eine Größe aus.';
  16. } else {
  17. $result = addCartItem($product['id'], $size);
  18. if (!$result['success']) {
  19. $error = 'Der Artikel konnte nicht in den Warenkorb gelegt werden.';
  20. } elseif ($result['status'] === 'replaced') {
  21. setFlashMessage('cart_notice', 'success', 'Die Größe für diesen Artikel wurde im Warenkorb aktualisiert.');
  22. header('Location: cart.php');
  23. exit;
  24. } elseif ($result['status'] === 'unchanged') {
  25. setFlashMessage('cart_notice', 'info', 'Dieser Artikel ist bereits mit der gewählten Größe im Warenkorb.');
  26. header('Location: cart.php');
  27. exit;
  28. } else {
  29. setFlashMessage('cart_notice', 'success', 'Der Artikel wurde zum Warenkorb hinzugefügt.');
  30. header('Location: cart.php');
  31. exit;
  32. }
  33. }
  34. }
  35. include __DIR__ . '/includes/header.php';
  36. ?>
  37. <?php if (isset($error)): ?>
  38. <div class="alert alert-error">
  39. <?php echo escape($error); ?>
  40. </div>
  41. <?php endif; ?>
  42. <div class="product-detail-grid">
  43. <div>
  44. <?php if (!empty($product['image']) && file_exists(__DIR__ . '/assets/images/' . $product['image'])): ?>
  45. <img class="product-image" src="<?php echo escape(SITE_URL); ?>/assets/images/<?php echo escape($product['image']); ?>" alt="<?php echo escape($product['name']); ?>">
  46. <?php else: ?>
  47. <div class="product-placeholder">Kein Bild verfügbar</div>
  48. <?php endif; ?>
  49. </div>
  50. <div class="product-copy">
  51. <h1><?php echo escape($product['name']); ?></h1>
  52. <div class="product-description-block">
  53. <h3>Beschreibung</h3>
  54. <p class="product-description"><?php echo nl2br(escape($product['description'])); ?></p>
  55. </div>
  56. <form method="POST" class="product-form">
  57. <?php if (!empty($sizes)): ?>
  58. <div class="form-group">
  59. <label for="size">Größe *</label>
  60. <select id="size" name="size" required onchange="updateAvailabilityNotice()">
  61. <option value="">Bitte wählen</option>
  62. <?php foreach ($sizes as $sizeOption): ?>
  63. <?php $label = getAvailabilityLabel($product, $sizeOption); ?>
  64. <option value="<?php echo escape($sizeOption); ?>" data-label="<?php echo escape($label); ?>">
  65. <?php echo escape($sizeOption); ?>
  66. </option>
  67. <?php endforeach; ?>
  68. </select>
  69. </div>
  70. <div id="availabilityNotice" class="alert alert-warning is-hidden"></div>
  71. <?php endif; ?>
  72. <button type="submit" name="add_to_cart" class="btn btn-block">In den Warenkorb</button>
  73. </form>
  74. <?php if (!empty($sizes)): ?>
  75. <script>
  76. function updateAvailabilityNotice() {
  77. const sizeSelect = document.getElementById('size');
  78. const notice = document.getElementById('availabilityNotice');
  79. const selectedOption = sizeSelect.options[sizeSelect.selectedIndex];
  80. const text = selectedOption ? selectedOption.getAttribute('data-label') : '';
  81. if (text) {
  82. notice.textContent = text;
  83. notice.classList.remove('is-hidden');
  84. } else {
  85. notice.textContent = '';
  86. notice.classList.add('is-hidden');
  87. }
  88. }
  89. </script>
  90. <?php endif; ?>
  91. <div class="mt-4">
  92. <a href="index.php" class="btn btn-secondary">Zurück zur Übersicht</a>
  93. </div>
  94. </div>
  95. </div>
  96. <?php include __DIR__ . '/includes/footer.php'; ?>