| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/checkout.php
- session_start();
- require_once __DIR__ . '/config/database.php';
- require_once __DIR__ . '/includes/mailer.php';
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_SESSION['cart'])) {
- $name = trim($_POST['customer_name']);
- $email = trim($_POST['customer_email']);
- $phone = trim($_POST['customer_phone']);
- $address = trim($_POST['shipping_address']);
- if (!empty($name) && !empty($email) && !empty($phone) && !empty($address)) {
- try {
- $pdo->beginTransaction();
- $ids = array_keys($_SESSION['cart']);
- $placeholders = implode(',', array_fill(0, count($ids), '?'));
-
- $stmt = $pdo->prepare("SELECT id, price, quantity FROM products WHERE id IN ($placeholders)");
- $stmt->execute($ids);
- $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
- $total_amount = 0;
- $items_data = [];
-
- foreach ($products as $p) {
- $prod_id = $p['id'];
- $cart_qty = $_SESSION['cart'][$prod_id];
-
- if ($p['quantity'] < $cart_qty) {
- throw new Exception("Το προϊόν με ID #{$prod_id} δεν έχει επαρκές απόθεμα.");
- }
- $total_amount += ($p['price'] * $cart_qty);
- $items_data[] = [
- 'product_id' => $prod_id,
- 'quantity' => $cart_qty,
- 'price' => $p['price']
- ];
- }
- // Εισαγωγή Παραγγελίας
- $orderStmt = $pdo->prepare("INSERT INTO orders (customer_name, customer_email, customer_phone, shipping_address, total_amount) VALUES (?, ?, ?, ?, ?)");
- $orderStmt->execute([$name, $email, $phone, $address, $total_amount]);
- $order_id = $pdo->lastInsertId();
- $itemStmt = $pdo->prepare("INSERT INTO order_items (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
-
- $stockStmt = $pdo->prepare("
- UPDATE products
- SET status = CASE WHEN (quantity - ?) <= 0 THEN 'sold' ELSE status END,
- quantity = quantity - ?
- WHERE id = ?
- ");
- foreach ($items_data as $item) {
- $itemStmt->execute([$order_id, $item['product_id'], $item['quantity'], $item['price']]);
- // Αφαίρεση από το απόθεμα
- $stockStmt->execute([$item['quantity'], $item['quantity'], $item['product_id']]);
- }
- $pdo->commit();
-
- // --- ΑΠΟΣΤΟΛΗ EMAIL ΜΕΤΑ ΤΗΝ ΕΠΙΤΥΧΗ ΚΑΤΑΧΩΡΗΣΗ ---
- $placeholders = [
- 'order_id' => $order_id,
- 'customer_name' => $name,
- 'total_amount' => number_format($total_amount, 2, ',', '.')
- ];
- // 1. Αποστολή Email στον Πελάτη (με το βασικό template)
- sendTemplateEmail($pdo, 'new_order', $email, $placeholders);
- // 2. Αποστολή Email στους Πωλητές (με το ξεχωριστό seller template)
- $sellersStmt = $pdo->prepare("
- SELECT DISTINCT u.email
- FROM order_items oi
- JOIN products p ON oi.product_id = p.id
- JOIN users u ON p.seller_id = u.id
- WHERE oi.order_id = ? AND u.email IS NOT NULL AND u.email != ''
- ");
- $sellersStmt->execute([$order_id]);
- $sellers = $sellersStmt->fetchAll(PDO::FETCH_ASSOC);
- foreach ($sellers as $seller) {
- sendTemplateEmail($pdo, 'seller_new_order', $seller['email'], $placeholders);
- }
- // ----------------------------------------------------
- unset($_SESSION['cart']);
- die("<div style='font-family:sans-serif; text-align:center; margin-top:50px;'>
- <h1 style='color: #198754;'>Η παραγγελία σου καταχωρήθηκε!</h1>
- <p>Ο κωδικός είναι: <strong>#{$order_id}</strong></p>
- <p>Το απόθεμα ενημερώθηκε και στάλθηκαν τα αντίστοιχα email. <a href='index.php'>Επιστροφή στην αρχική</a></p>
- </div>");
- } catch (Exception $e) {
- $pdo->rollBack();
- die("<div style='font-family:sans-serif; text-align:center; margin-top:50px; color: #dc3545;'>
- <h2>Σφάλμα: " . $e->getMessage() . "</h2><a href='cart.php'>Πίσω</a>
- </div>");
- }
- }
- }
- header("Location: index.php");
- exit;
|