| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/cart.php
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- require_once __DIR__ . '/vendor/autoload.php';
- require_once __DIR__ . '/config/database.php';
- require_once __DIR__ . '/config/functions.php';
- // --- Μηχανισμός Αλλαγής Γλώσσας ---
- if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
- $_SESSION['lang'] =$_GET['lang'];
- }
- $current_lang =$_SESSION['lang'] ?? 'el';
- // Αρχικοποίηση καλαθιού αν δεν υπάρχει
- if (!isset($_SESSION['cart'])) {$_SESSION['cart'] = [];
- }
- // Χειρισμός Ενεργειών (Προσθήκη, Διαγραφή)
- $action = isset($_GET['action']) ?$_GET['action'] : '';
- if ($action === 'add') {$product_id = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0;
- $qty = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 1;
- if ($product_id > 0) {
- if (isset($_SESSION['cart'][$product_id])) {$_SESSION['cart'][$product_id] +=$qty;
- } else {
- $_SESSION['cart'][$product_id] =$qty;
- }
- }
- header("Location: cart.php");
- exit;
- }
- if ($action === 'remove') {$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
- if (isset($_SESSION['cart'][$product_id])) {
- unset($_SESSION['cart'][$product_id]);
- }
- header("Location: cart.php");
- exit;
- }
- // Δυναμική ανάγνωση ενεργού theme
- $active_theme = 'default';
- try {
- $stmt =$pdo->query("SELECT value FROM settings WHERE `key` = 'active_theme'");
- $res =$stmt->fetchColumn();
- if ($res) { $active_theme =$res; }
- } catch (Exception $e) {}
- $theme_dir = __DIR__ . '/themes/' .$active_theme;
- if (!is_dir($theme_dir)) {
- $active_theme = 'default';$theme_dir = __DIR__ . '/themes/default';
- }
- $loader = new \Twig\Loader\FilesystemLoader($theme_dir);
- $twig = new \Twig\Environment($loader, ['cache' => false]);
- $twig->addGlobal('session',$_SESSION);
- $twig->addGlobal('current_lang',$current_lang);
- // Διόρθωση: Ανάγνωση κατηγοριών με τα δίγλωσσα πεδία (name_el / name_en)
- try {
- $categoriesRaw =$pdo->query("SELECT * FROM categories ORDER BY sort_order ASC, name_el ASC")->fetchAll(PDO::FETCH_ASSOC);
- $categories = [];
- foreach ($categoriesRaw as $cat) {$cat['name'] = ($current_lang === 'en' && !empty($cat['name_en'])) ? $cat['name_en'] :$cat['name_el'];
- $categories[] =$cat;
- }
- } catch (Exception $e) {$categories = [];
- }
- // Φέρνουμε τα στοιχεία των προϊόντων που βρίσκονται στο καλάθι από τη βάση
- $cart_items = [];$total_price = 0;
- if (!empty($_SESSION['cart'])) {
- $ids = array_keys($_SESSION['cart']);
- $placeholders = implode(',', array_fill(0, count($ids), '?'));
-
- $stmt =$pdo->prepare("
- SELECT p.id, p.title, p.price, pi.image_path
- FROM products p
- LEFT JOIN product_images pi ON (p.id = pi.product_id AND pi.is_primary = 1)
- WHERE p.id IN ($placeholders)
- ");
- $stmt->execute($ids);
- $products_in_db =$stmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($products_in_db as$prod) {
- $prod_id =$prod['id'];
- $qty =$_SESSION['cart'][$prod_id];$subtotal = $prod['price'] *$qty;
- $total_price +=$subtotal;
- $cart_items[] = [
- 'id' => $prod_id,
- 'title' => $prod['title'],
- 'price' => $prod['price'],
- 'image_path' => $prod['image_path'],
- 'quantity' => $qty,
- 'subtotal' => $subtotal
- ];
- }
- }
- echo $twig->render('cart.html.twig', [
- 'site_title' => 'Hockey Gear CMS',
- 'categories' => $categories,
- 'cart_items' => $cart_items,
- 'total_price' => $total_price
- ]);
|