checkout.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. require_once __DIR__ . '/config.php';
  3. require_once __DIR__ . '/includes/functions.php';
  4. $pageTitle = 'Reservierung';
  5. $cart = $_SESSION['cart'] ?? [];
  6. if (empty($cart)) {
  7. header('Location: cart.php');
  8. exit;
  9. }
  10. // Validate cart items and stock
  11. $cartItems = [];
  12. $errors = [];
  13. $total = 0;
  14. foreach ($cart as $item) {
  15. $product = getProductById($item['product_id']);
  16. if (!$product) {
  17. $errors[] = 'Ein Produkt wurde nicht gefunden.';
  18. continue;
  19. }
  20. $size = isset($item['size']) ? $item['size'] : null;
  21. if (!checkStock($item['product_id'], $item['quantity'], $size)) {
  22. $sizeInfo = $size ? " (Größe: $size)" : '';
  23. $errors[] = 'Nicht genügend Lagerbestand für: ' . $product['name'] . $sizeInfo;
  24. continue;
  25. }
  26. $itemTotal = $product['price'] * $item['quantity'];
  27. $total += $itemTotal;
  28. $cartItems[] = [
  29. 'product' => $product,
  30. 'quantity' => $item['quantity'],
  31. 'total' => $itemTotal,
  32. 'size' => isset($item['size']) ? $item['size'] : null
  33. ];
  34. }
  35. // Handle form submission
  36. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_reservation'])) {
  37. $customerName = sanitize($_POST['customer_name'] ?? '');
  38. $customerEmail = sanitize($_POST['customer_email'] ?? '');
  39. if (empty($customerName)) {
  40. $errors[] = 'Bitte geben Sie Ihren Namen ein.';
  41. }
  42. if (empty($customerEmail) || !filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
  43. $errors[] = 'Bitte geben Sie eine gültige E-Mail-Adresse ein.';
  44. }
  45. if (empty($errors)) {
  46. // Create reservation
  47. $items = [];
  48. foreach ($cart as $cartItem) {
  49. $item = [
  50. 'product_id' => $cartItem['product_id'],
  51. 'quantity' => $cartItem['quantity']
  52. ];
  53. if (isset($cartItem['size']) && !empty($cartItem['size'])) {
  54. $item['size'] = $cartItem['size'];
  55. }
  56. $items[] = $item;
  57. }
  58. $result = createReservation($customerName, $customerEmail, $items);
  59. if ($result['success']) {
  60. // Clear cart
  61. $_SESSION['cart'] = [];
  62. // Redirect to reservation confirmation
  63. header('Location: reservation.php?code=' . urlencode($result['reservation']['code']));
  64. exit;
  65. } else {
  66. $errors[] = $result['message'];
  67. }
  68. }
  69. }
  70. include __DIR__ . '/includes/header.php';
  71. ?>
  72. <h2>Reservierung abschließen</h2>
  73. <?php if (!empty($errors)): ?>
  74. <div class="alert alert-error">
  75. <ul style="margin-left: 1.5rem;">
  76. <?php foreach ($errors as $error): ?>
  77. <li><?php echo htmlspecialchars($error); ?></li>
  78. <?php endforeach; ?>
  79. </ul>
  80. </div>
  81. <?php endif; ?>
  82. <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-top: 2rem;">
  83. <div>
  84. <h3>Ihre Bestellung</h3>
  85. <?php foreach ($cartItems as $cartItem): ?>
  86. <div style="background: white; padding: 1rem; margin-bottom: 1rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
  87. <strong><?php echo htmlspecialchars($cartItem['product']['name']); ?></strong><br>
  88. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  89. Größe: <?php echo htmlspecialchars($cartItem['size']); ?><br>
  90. <?php endif; ?>
  91. Menge: <?php echo $cartItem['quantity']; ?><br>
  92. Preis: <?php echo formatPrice($cartItem['total']); ?>
  93. </div>
  94. <?php endforeach; ?>
  95. <div style="background: white; padding: 1rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-top: 1rem;">
  96. <strong style="font-size: 1.2rem;">Gesamtsumme: <?php echo formatPrice($total); ?></strong>
  97. </div>
  98. </div>
  99. <div>
  100. <h3>Ihre Daten</h3>
  101. <form method="POST">
  102. <div class="form-group">
  103. <label for="customer_name">Name *</label>
  104. <input type="text" id="customer_name" name="customer_name" required
  105. value="<?php echo isset($_POST['customer_name']) ? htmlspecialchars($_POST['customer_name']) : ''; ?>">
  106. </div>
  107. <div class="form-group">
  108. <label for="customer_email">E-Mail-Adresse *</label>
  109. <input type="email" id="customer_email" name="customer_email" required
  110. value="<?php echo isset($_POST['customer_email']) ? htmlspecialchars($_POST['customer_email']) : ''; ?>">
  111. </div>
  112. <div class="alert alert-info">
  113. <strong>Hinweis:</strong> Nach der Reservierung erhalten Sie einen Abholcode, den Sie bei der Abholung vorzeigen müssen.
  114. Die Reservierung ist <?php echo RESERVATION_EXPIRY_DAYS; ?> Tage gültig.
  115. </div>
  116. <button type="submit" name="create_reservation" class="btn" style="width: 100%;">Reservierung abschließen</button>
  117. </form>
  118. <div style="margin-top: 1rem;">
  119. <a href="cart.php" class="btn btn-secondary">Zurück zum Warenkorb</a>
  120. </div>
  121. </div>
  122. </div>
  123. <?php include __DIR__ . '/includes/footer.php'; ?>