cart.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/cart.php
  3. if (session_status() === PHP_SESSION_NONE) {
  4. session_start();
  5. }
  6. require_once __DIR__ . '/vendor/autoload.php';
  7. require_once __DIR__ . '/config/database.php';
  8. require_once __DIR__ . '/config/functions.php';
  9. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  10. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  11. $_SESSION['lang'] =$_GET['lang'];
  12. }
  13. $current_lang =$_SESSION['lang'] ?? 'el';
  14. // Αρχικοποίηση καλαθιού αν δεν υπάρχει
  15. if (!isset($_SESSION['cart'])) {$_SESSION['cart'] = [];
  16. }
  17. // Χειρισμός Ενεργειών (Προσθήκη, Διαγραφή)
  18. $action = isset($_GET['action']) ?$_GET['action'] : '';
  19. if ($action === 'add') {$product_id = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0;
  20. $qty = isset($_POST['quantity']) ? (int)$_POST['quantity'] : 1;
  21. if ($product_id > 0) {
  22. if (isset($_SESSION['cart'][$product_id])) {$_SESSION['cart'][$product_id] +=$qty;
  23. } else {
  24. $_SESSION['cart'][$product_id] =$qty;
  25. }
  26. }
  27. header("Location: cart.php");
  28. exit;
  29. }
  30. if ($action === 'remove') {$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  31. if (isset($_SESSION['cart'][$product_id])) {
  32. unset($_SESSION['cart'][$product_id]);
  33. }
  34. header("Location: cart.php");
  35. exit;
  36. }
  37. // Δυναμική ανάγνωση ενεργού theme
  38. $active_theme = 'default';
  39. try {
  40. $stmt =$pdo->query("SELECT value FROM settings WHERE `key` = 'active_theme'");
  41. $res =$stmt->fetchColumn();
  42. if ($res) { $active_theme =$res; }
  43. } catch (Exception $e) {}
  44. $theme_dir = __DIR__ . '/themes/' .$active_theme;
  45. if (!is_dir($theme_dir)) {
  46. $active_theme = 'default';$theme_dir = __DIR__ . '/themes/default';
  47. }
  48. $loader = new \Twig\Loader\FilesystemLoader($theme_dir);
  49. $twig = new \Twig\Environment($loader, ['cache' => false]);
  50. $twig->addGlobal('session',$_SESSION);
  51. $twig->addGlobal('current_lang',$current_lang);
  52. // Διόρθωση: Ανάγνωση κατηγοριών με τα δίγλωσσα πεδία (name_el / name_en)
  53. try {
  54. $categoriesRaw =$pdo->query("SELECT * FROM categories ORDER BY sort_order ASC, name_el ASC")->fetchAll(PDO::FETCH_ASSOC);
  55. $categories = [];
  56. foreach ($categoriesRaw as $cat) {$cat['name'] = ($current_lang === 'en' && !empty($cat['name_en'])) ? $cat['name_en'] :$cat['name_el'];
  57. $categories[] =$cat;
  58. }
  59. } catch (Exception $e) {$categories = [];
  60. }
  61. // Φέρνουμε τα στοιχεία των προϊόντων που βρίσκονται στο καλάθι από τη βάση
  62. $cart_items = [];$total_price = 0;
  63. if (!empty($_SESSION['cart'])) {
  64. $ids = array_keys($_SESSION['cart']);
  65. $placeholders = implode(',', array_fill(0, count($ids), '?'));
  66. $stmt =$pdo->prepare("
  67. SELECT p.id, p.title, p.price, pi.image_path
  68. FROM products p
  69. LEFT JOIN product_images pi ON (p.id = pi.product_id AND pi.is_primary = 1)
  70. WHERE p.id IN ($placeholders)
  71. ");
  72. $stmt->execute($ids);
  73. $products_in_db =$stmt->fetchAll(PDO::FETCH_ASSOC);
  74. foreach ($products_in_db as$prod) {
  75. $prod_id =$prod['id'];
  76. $qty =$_SESSION['cart'][$prod_id];$subtotal = $prod['price'] *$qty;
  77. $total_price +=$subtotal;
  78. $cart_items[] = [
  79. 'id' => $prod_id,
  80. 'title' => $prod['title'],
  81. 'price' => $prod['price'],
  82. 'image_path' => $prod['image_path'],
  83. 'quantity' => $qty,
  84. 'subtotal' => $subtotal
  85. ];
  86. }
  87. }
  88. echo $twig->render('cart.html.twig', [
  89. 'site_title' => 'Hockey Gear CMS',
  90. 'categories' => $categories,
  91. 'cart_items' => $cart_items,
  92. 'total_price' => $total_price
  93. ]);