checkout.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/checkout.php
  3. session_start();
  4. require_once __DIR__ . '/config/database.php';
  5. require_once __DIR__ . '/includes/mailer.php';
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_SESSION['cart'])) {
  7. $name = trim($_POST['customer_name']);
  8. $email = trim($_POST['customer_email']);
  9. $phone = trim($_POST['customer_phone']);
  10. $address = trim($_POST['shipping_address']);
  11. if (!empty($name) && !empty($email) && !empty($phone) && !empty($address)) {
  12. try {
  13. $pdo->beginTransaction();
  14. $ids = array_keys($_SESSION['cart']);
  15. $placeholders = implode(',', array_fill(0, count($ids), '?'));
  16. $stmt = $pdo->prepare("SELECT id, price, quantity FROM products WHERE id IN ($placeholders)");
  17. $stmt->execute($ids);
  18. $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
  19. $total_amount = 0;
  20. $items_data = [];
  21. foreach ($products as $p) {
  22. $prod_id = $p['id'];
  23. $cart_qty = $_SESSION['cart'][$prod_id];
  24. if ($p['quantity'] < $cart_qty) {
  25. throw new Exception("Το προϊόν με ID #{$prod_id} δεν έχει επαρκές απόθεμα.");
  26. }
  27. $total_amount += ($p['price'] * $cart_qty);
  28. $items_data[] = [
  29. 'product_id' => $prod_id,
  30. 'quantity' => $cart_qty,
  31. 'price' => $p['price']
  32. ];
  33. }
  34. // Εισαγωγή Παραγγελίας
  35. $orderStmt = $pdo->prepare("INSERT INTO orders (customer_name, customer_email, customer_phone, shipping_address, total_amount) VALUES (?, ?, ?, ?, ?)");
  36. $orderStmt->execute([$name, $email, $phone, $address, $total_amount]);
  37. $order_id = $pdo->lastInsertId();
  38. $itemStmt = $pdo->prepare("INSERT INTO order_items (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
  39. $stockStmt = $pdo->prepare("
  40. UPDATE products
  41. SET status = CASE WHEN (quantity - ?) <= 0 THEN 'sold' ELSE status END,
  42. quantity = quantity - ?
  43. WHERE id = ?
  44. ");
  45. foreach ($items_data as $item) {
  46. $itemStmt->execute([$order_id, $item['product_id'], $item['quantity'], $item['price']]);
  47. // Αφαίρεση από το απόθεμα
  48. $stockStmt->execute([$item['quantity'], $item['quantity'], $item['product_id']]);
  49. }
  50. $pdo->commit();
  51. // --- ΑΠΟΣΤΟΛΗ EMAIL ΜΕΤΑ ΤΗΝ ΕΠΙΤΥΧΗ ΚΑΤΑΧΩΡΗΣΗ ---
  52. $placeholders = [
  53. 'order_id' => $order_id,
  54. 'customer_name' => $name,
  55. 'total_amount' => number_format($total_amount, 2, ',', '.')
  56. ];
  57. // 1. Αποστολή Email στον Πελάτη (με το βασικό template)
  58. sendTemplateEmail($pdo, 'new_order', $email, $placeholders);
  59. // 2. Αποστολή Email στους Πωλητές (με το ξεχωριστό seller template)
  60. $sellersStmt = $pdo->prepare("
  61. SELECT DISTINCT u.email
  62. FROM order_items oi
  63. JOIN products p ON oi.product_id = p.id
  64. JOIN users u ON p.seller_id = u.id
  65. WHERE oi.order_id = ? AND u.email IS NOT NULL AND u.email != ''
  66. ");
  67. $sellersStmt->execute([$order_id]);
  68. $sellers = $sellersStmt->fetchAll(PDO::FETCH_ASSOC);
  69. foreach ($sellers as $seller) {
  70. sendTemplateEmail($pdo, 'seller_new_order', $seller['email'], $placeholders);
  71. }
  72. // ----------------------------------------------------
  73. unset($_SESSION['cart']);
  74. die("<div style='font-family:sans-serif; text-align:center; margin-top:50px;'>
  75. <h1 style='color: #198754;'>Η παραγγελία σου καταχωρήθηκε!</h1>
  76. <p>Ο κωδικός είναι: <strong>#{$order_id}</strong></p>
  77. <p>Το απόθεμα ενημερώθηκε και στάλθηκαν τα αντίστοιχα email. <a href='index.php'>Επιστροφή στην αρχική</a></p>
  78. </div>");
  79. } catch (Exception $e) {
  80. $pdo->rollBack();
  81. die("<div style='font-family:sans-serif; text-align:center; margin-top:50px; color: #dc3545;'>
  82. <h2>Σφάλμα: " . $e->getMessage() . "</h2><a href='cart.php'>Πίσω</a>
  83. </div>");
  84. }
  85. }
  86. }
  87. header("Location: index.php");
  88. exit;