render('front/cart/empty.html.twig', [ 'title' => 'Warenkorb - Webshop' ]); return; } // DB-Verbindung herstellen $connectionParams = [ 'dbname' => getenv('DB_DATABASE') ?: 'freeshop', 'user' => getenv('DB_USERNAME') ?: 'freeshop_user', 'password' => getenv('DB_PASSWORD') ?: 'freeshop_password', 'host' => getenv('DB_HOST') ?: 'db', 'driver' => 'pdo_mysql', 'port' => getenv('DB_PORT') ?: 3306, 'charset' => 'utf8mb4', ]; try { $conn = DriverManager::getConnection($connectionParams); // Produkte aus Warenkorb laden $cartItems = []; $total = 0; foreach ($cart as $productId => $quantity) { $stmt = $conn->prepare('SELECT * FROM ws_product WHERE id = ?'); $stmt->execute([$productId]); $product = $stmt->fetchAssociative(); if ($product) { $product['quantity'] = $quantity; $product['subtotal'] = $product['price'] * $quantity; $cartItems[] = $product; $total += $product['subtotal']; } } $this->render('front/cart/index.html.twig', [ 'title' => 'Warenkorb - Webshop', 'cart_items' => $cartItems, 'total' => $total ]); } catch (Exception $e) { $this->render('front/cart/index.html.twig', [ 'title' => 'Warenkorb - Webshop', 'cart_items' => [], 'total' => 0, 'error' => 'Datenbankfehler: ' . $e->getMessage() ]); } } public function add() { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: /cart'); exit; } $productId = $_POST['product_id'] ?? 0; $quantity = (int)($_POST['quantity'] ?? 1); if ($productId <= 0 || $quantity <= 0) { header('Location: /products?error=Ungültige Produktdaten'); exit; } // Session starten session_start(); // Warenkorb initialisieren if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = []; } // Produkt zum Warenkorb hinzufügen if (isset($_SESSION['cart'][$productId])) { $_SESSION['cart'][$productId] += $quantity; } else { $_SESSION['cart'][$productId] = $quantity; } header('Location: /cart?success=Produkt zum Warenkorb hinzugefügt'); exit; } public function update() { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: /cart'); exit; } $productId = $_POST['product_id'] ?? 0; $quantity = (int)($_POST['quantity'] ?? 0); // Session starten session_start(); if ($quantity <= 0) { // Produkt aus Warenkorb entfernen unset($_SESSION['cart'][$productId]); } else { // Menge aktualisieren $_SESSION['cart'][$productId] = $quantity; } header('Location: /cart?success=Warenkorb aktualisiert'); exit; } public function remove() { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: /cart'); exit; } $productId = $_POST['product_id'] ?? 0; // Session starten session_start(); // Produkt aus Warenkorb entfernen unset($_SESSION['cart'][$productId]); header('Location: /cart?success=Produkt aus Warenkorb entfernt'); exit; } public function clear() { // Session starten session_start(); // Warenkorb leeren $_SESSION['cart'] = []; header('Location: /cart?success=Warenkorb geleert'); exit; } protected function render($template, $data = []) { // Einfache Template-Engine (später durch Twig ersetzen) extract($data); include __DIR__ . '/../../templates/' . $template; } }