106 lines
3.5 KiB
HTML
106 lines
3.5 KiB
HTML
<!--
|
||
Diese Seite zeigt alle Motorrad-Produkte des Webshops an.
|
||
Produkte werden dynamisch von der API geladen.
|
||
Bei einem Fehler wird eine passende Fehlermeldung angezeigt.
|
||
-->
|
||
|
||
<!DOCTYPE html>
|
||
<html lang="de">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Shop - Motorräder</title>
|
||
<!-- Haupt-CSS-Datei -->
|
||
<link rel="stylesheet" href="/Styles/styles-main.css">
|
||
<!-- Icons -->
|
||
<link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet">
|
||
<!-- Script zum Laden von Header/Footer -->
|
||
<script src="/header_footer"></script>
|
||
</head>
|
||
<body>
|
||
<div class="wrapper">
|
||
<!-- Header -->
|
||
<div id="header"></div>
|
||
|
||
<!-- Hauptinhalt -->
|
||
<main class="main-content">
|
||
<section style="padding: 0px 30px; text-align: left;">
|
||
<h1>Unsere Motorrad Produkte</h1>
|
||
</section>
|
||
|
||
<section class="card-grid" id="products_motorrad">
|
||
<!-- Produkte -->
|
||
</section>
|
||
</main>
|
||
|
||
<!-- Fußzeile -->
|
||
<div id="footer"></div>
|
||
</div>
|
||
|
||
<script>
|
||
// Funktion zum Hinzufügen zum Warenkorb
|
||
function zumWarenkorbHinzufuegen(product) {
|
||
let warenkorb = JSON.parse(localStorage.getItem('warenkorb')) || [];
|
||
|
||
const existingProduct = warenkorb.find(p => p.product_id === product.id);
|
||
|
||
if (existingProduct) {
|
||
existingProduct.quantity += 1; // Wenn schon vorhanden, Anzahl erhöhen
|
||
} else {
|
||
warenkorb.push({
|
||
product_id: product.id,
|
||
product_name: product.name,
|
||
price: product.price,
|
||
quantity: 1
|
||
});
|
||
}
|
||
|
||
localStorage.setItem('warenkorb', JSON.stringify(warenkorb));
|
||
alert(`${product.name} wurde zum Warenkorb hinzugefügt!`);
|
||
zeigeWarenkorbAnzahl();
|
||
}
|
||
|
||
// Produkte laden
|
||
fetch('/api/products/motorrad')
|
||
.then(res => res.json())
|
||
.then(products => {
|
||
const container = document.getElementById('products_motorrad');
|
||
container.innerHTML = '';
|
||
|
||
// Für jedes Produkt eine Karte erstellen
|
||
products.forEach(product => {
|
||
const card = document.createElement('div');
|
||
card.classList.add('card');
|
||
card.innerHTML = `
|
||
<img src="${product.image_url}" alt="${product.name}">
|
||
<h3>${product.name}</h3>
|
||
<p>Preis: ${product.price.toFixed(2)} €</p>
|
||
<p>${product.description}</p>
|
||
<p>Artikel Nr: ${product.id}</p>
|
||
<button class="add-to-cart">Zum Warenkorb hinzufügen</button>
|
||
`;
|
||
|
||
const addToCartButton = card.querySelector('.add-to-cart');
|
||
addToCartButton.addEventListener('click', () => {
|
||
zumWarenkorbHinzufuegen(product);
|
||
});
|
||
|
||
container.appendChild(card);
|
||
});
|
||
})
|
||
// Fehlermeldung
|
||
.catch(err => {
|
||
console.error('Fehler beim Laden der Produkte:', err);
|
||
|
||
const container = document.getElementById('products_motorrad');
|
||
container.innerHTML = `
|
||
<div class="error-message">
|
||
<h3>Fehler beim Laden der Produkte</h3>
|
||
<p>Es gab ein Problem beim Abrufen der Produktdaten.<br>
|
||
Wir arbeiten bereits daran – bitte versuchen Sie es später erneut.</p>
|
||
</div>`;
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|