templates/Shop/checkout.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}Checkout{% endblock %}
  3. {% block body %}
  4. <div class="container my-5">
  5.     <!-- Hauptcontainer -->
  6.     <div class="row">
  7.         
  8.         <!-- Produktübersicht -->
  9.         <section class="col-md-8">
  10.             <div class="card h-100 shadow-sm">
  11.                 <div class="card-body">
  12.                     {% if cart is not empty %}
  13.                         <h1 class="mb-4 product-name">{{ product.name }}</h1>
  14.                         <p class="product-description">{{ product.beschreibung }}</p>
  15.                         <p><strong>Preis: <span class="product-price">{{ product.preis|number_format(2, ',', '.') }} €</span></strong></p>
  16.                     {% else %}
  17.                         <h1 class="mb-4">Keine Produkte im Warenkorb</h1>
  18.                         <p>Bitte fügen Sie ein Produkt hinzu, um es im Warenkorb anzuzeigen.</p>
  19.                     {% endif %}
  20.                 </div>
  21.             </div>
  22.         </section>
  23.         
  24.         <!-- Kasse -->
  25.         <section class="col-md-4">
  26.             <div class="card h-100 shadow-sm">
  27.                 <div class="card-body d-flex flex-column justify-content-between">
  28.                     <div>
  29.                         <h4 class="mb-4">Kasse</h4>
  30.                         <div id="cart-items">
  31.                             {% if cart is not empty %}
  32.                                 {% set total = 0 %}
  33.                                 <ul class="list-group mb-3">
  34.                                     {% for cart_item in cart %}
  35.                                         <li class="list-group-item d-flex justify-content-between align-items-center">
  36.                                             <span>{{ cart_item.product.name }}</span>
  37.                                             <div>
  38.                                                 <span>{{ cart_item.product.preis|number_format(2, ',', '.') }} € x {{ cart_item.quantity }}</span>
  39.                                                 <button class="btn btn-sm btn-danger ms-3 remove-from-cart" data-id="{{ cart_item.product.id }}">-</button>
  40.                                             </div>
  41.                                         </li>
  42.                                         {% set total = total + (cart_item.product.preis * cart_item.quantity) %}
  43.                                     {% endfor %}
  44.                                 </ul>
  45.                                 <div class="d-flex justify-content-between mb-3">
  46.                                     <strong>Gesamt:</strong>
  47.                                     <strong id="cart-total">{{ total|number_format(2, ',', '.') }} €</strong>
  48.                                 </div>
  49.                             {% else %}
  50.                                 <div class="alert alert-warning">
  51.                                     Ihr Warenkorb ist leer.
  52.                                 </div>
  53.                             {% endif %}
  54.                         </div>
  55.                     </div>
  56.                     
  57.                     <!-- Auswahl der Zahlungsmethoden -->
  58.                     <form method="post" action="{{ path('shop_checkout_process') }}">
  59.                         <div class="form-group mb-3">
  60.                             <label for="payment_method">Zahlungsmethode auswählen:</label>
  61.                             <select name="payment_method" id="payment_method" class="form-select">
  62.                                 <option value="paypal">PayPal</option>
  63.                                 <option value="stripe">Kreditkarte (Stripe)</option>
  64.                             </select>
  65.                         </div>
  66.                         <button type="submit" class="btn btn-primary btn-lg w-100 mt-3" {% if cart is empty %}disabled{% endif %}>Bezahlen</button>
  67.                     </form>
  68.                 </div>
  69.             </div>
  70.         </section>
  71.     </div>
  72. </div>
  73. <!-- Ähnliche Produkte (über volle Breite) -->
  74. <section class="py-5" style="background-color: #f8f9fa;">
  75.     <div class="container">
  76.         <h2 class="text-center mb-4">Diese Produkte könnten Ihnen auch gefallen</h2>
  77.         <div class="row">
  78.             {% for similar_product in similar_products %}
  79.                 <div class="col-md-6 mb-4">
  80.                     <div class="card h-100 shadow-sm">
  81.                         <div class="card-body">
  82.                             <h5 class="card-title">{{ similar_product.name }}</h5>
  83.                             <p class="card-text">{{ similar_product.beschreibung|length > 50 ? similar_product.beschreibung|slice(0, 50) ~ '...' : similar_product.beschreibung }}</p>
  84.                             <p class="card-text">
  85.                                 <strong>{{ similar_product.preis|number_format(2, ',', '.') }} €</strong>
  86.                             </p>
  87.                         </div>
  88.                         <div class="card-footer bg-white">
  89.                             <button class="btn btn-secondary w-100 add-to-cart" data-id="{{ similar_product.id }}">Hinzufügen</button>
  90.                         </div>
  91.                     </div>
  92.                 </div>
  93.             {% else %}
  94.                 <div class="col-12">
  95.                     <div class="alert alert-warning text-center">
  96.                         Keine ähnlichen Produkte gefunden.
  97.                     </div>
  98.                 </div>
  99.             {% endfor %}
  100.         </div>
  101.     </div>
  102. </section>
  103. <!-- AJAX Script -->
  104. <script>
  105.     document.addEventListener('DOMContentLoaded', function() {
  106.         // Hinzufügen von Produkten
  107.         document.querySelectorAll('.add-to-cart').forEach(button => {
  108.             button.addEventListener('click', function() {
  109.                 let productId = this.getAttribute('data-id');
  110.                 fetch('/shop/cart/add/' + productId, {
  111.                     method: 'POST',
  112.                     headers: {
  113.                         'Content-Type': 'application/json',
  114.                         'X-Requested-With': 'XMLHttpRequest'
  115.                     }
  116.                 })
  117.                 .then(response => response.json())
  118.                 .then(data => {
  119.                     // Aktualisiere den Warenkorb-Bereich
  120.                     document.getElementById('cart-items').innerHTML = data.cartHtml;
  121.                     document.getElementById('cart-total').innerText = data.total + ' €';
  122.                     // Aktualisiere die Produktinformationen, wenn der Warenkorb nicht leer ist
  123.                     if (data.product) {
  124.                         document.querySelector('.product-name').innerText = data.product.name;
  125.                         document.querySelector('.product-description').innerText = data.product.description;
  126.                         document.querySelector('.product-price').innerText = data.product.price + ' €';
  127.                     }
  128.                 });
  129.             });
  130.         });
  131.         // Entfernen von Produkten
  132.         document.addEventListener('click', function(event) {
  133.             if (event.target.classList.contains('remove-from-cart')) {
  134.                 let productId = event.target.getAttribute('data-id');
  135.                 fetch('/shop/cart/remove/' + productId, {
  136.                     method: 'POST',
  137.                     headers: {
  138.                         'Content-Type': 'application/json',
  139.                         'X-Requested-With': 'XMLHttpRequest'
  140.                     }
  141.                 })
  142.                 .then(response => response.json())
  143.                 .then(data => {
  144.                     // Aktualisiere den Warenkorb-Bereich
  145.                     document.getElementById('cart-items').innerHTML = data.cartHtml;
  146.                     document.getElementById('cart-total').innerText = data.total + ' €';
  147.                     // Wenn der Warenkorb leer ist, zeige Nachricht an
  148.                     if (Object.keys(data.cart).length === 0) {
  149.                         document.querySelector('.product-name').innerText = 'Keine Produkte im Warenkorb';
  150.                         document.querySelector('.product-description').innerText = '';
  151.                         document.querySelector('.product-price').innerText = '';
  152.                     }
  153.                 });
  154.             }
  155.         });
  156.     });
  157. </script>
  158. {% endblock %}