prepare("SELECT id, price FROM products WHERE id IN ($placeholders)"); $stmt->execute($product_ids); $products_from_db = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE); $total_amount = 0; foreach ($_SESSION['cart'] as $product_id => $quantity) { if(isset($products_from_db[$product_id])){ $total_amount += $products_from_db[$product_id]['price'] * $quantity; } } // --- Database Transaction --- $pdo->beginTransaction(); // 1. Insert into orders table $sql_order = "INSERT INTO orders (customer_name, customer_email, customer_address, total_amount) VALUES (?, ?, ?, ?)"; $stmt_order = $pdo->prepare($sql_order); $stmt_order->execute([$name, $email, $address, $total_amount]); $order_id = $pdo->lastInsertId(); // 2. Insert into order_items table $sql_items = "INSERT INTO order_items (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)"; $stmt_items = $pdo->prepare($sql_items); foreach ($_SESSION['cart'] as $product_id => $quantity) { if(isset($products_from_db[$product_id])){ $price = $products_from_db[$product_id]['price']; $stmt_items->execute([$order_id, $product_id, $quantity, $price]); } } // 3. Commit the transaction $pdo->commit(); // 4. Clear the cart and set success flag unset($_SESSION['cart']); $order_placed_successfully = true; $p_title = "سفارش شما ثبت شد"; } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } error_log("Checkout Error: " . $e->getMessage()); $error_message = 'مشکلی در ثبت سفارش شما به وجود آمد. لطفاً دوباره تلاش کنید.'; } } } ?> <?php echo $p_title; ?> - چرم آتیمه

از خرید شما متشکریم!

سفارش شما با موفقیت ثبت شد و به زودی پردازش خواهد شد. یک ایمیل تایید برای شما ارسال گردید.

بازگشت به فروشگاه
.
اطلاعات ارسال