= FREE_SHIPPING_MIN ? 0 : SHIPPING_COST; $tax = ($subtotal * TAX_RATE) / 100; $discount = 0; $coupon_code = null; if (isset($_SESSION['coupon'])) { $discount = calculate_discount($_SESSION['coupon'], $subtotal); $coupon_code = $_SESSION['coupon']['code']; } $total = $subtotal + $shipping_cost + $tax - $discount; $order_number = generate_order_number(); try { $db->beginTransaction(); $user_id = is_logged_in() ? $_SESSION['user_id'] : null; $stmt = $db->prepare("INSERT INTO orders (order_number, user_id, first_name, last_name, email, phone, address_line1, address_line2, city, state, zip_code, country, shipping_first_name, shipping_last_name, shipping_address_line1, shipping_address_line2, shipping_city, shipping_state, shipping_zip_code, shipping_country, shipping_cost, subtotal, discount, coupon_code, tax, total, payment_method, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$order_number, $user_id, $first_name, $last_name, $email, $phone, $address_line1, $address_line2, $city, $state, $zip_code, $country, $shipping_first_name, $shipping_last_name, $shipping_address_line1, $shipping_address_line2, $shipping_city, $shipping_state, $shipping_zip_code, $shipping_country, $shipping_cost, $subtotal, $discount, $coupon_code, $tax, $total, $payment_method, $notes]); $order_id = $db->lastInsertId(); foreach ($cart_items as $item) { $price = $item['sale_price'] > 0 ? $item['sale_price'] : $item['regular_price']; $stmt = $db->prepare("INSERT INTO order_items (order_id, product_id, product_name, product_image, variation_details, quantity, price, total) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$order_id, $item['id'], $item['name'], $item['thumbnail'], $item['variation_details'], $item['quantity'], $price, $price * $item['quantity']]); // Update stock $db->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?")->execute([$item['quantity'], $item['id']]); } $db->commit(); // Clear cart and coupon unset($_SESSION['cart']); unset($_SESSION['coupon']); // Update coupon usage if ($coupon_code) { $db->prepare("UPDATE coupons SET used_count = used_count + 1 WHERE code = ?")->execute([$coupon_code]); } header('Location: ' . SITE_URL . 'pages/order-success.php?order=' . $order_number); exit; } catch (Exception $e) { $db->rollBack(); set_flash('error', 'Order placement failed. Please try again.'); } } $cart_total = get_cart_total(); $shipping_cost = $cart_total >= FREE_SHIPPING_MIN ? 0 : SHIPPING_COST; $tax = ($cart_total * TAX_RATE) / 100; $discount = isset($_SESSION['coupon']) ? calculate_discount($_SESSION['coupon'], $cart_total) : 0; $grand_total = $cart_total + $shipping_cost + $tax - $discount; $current_user = is_logged_in() ? get_user_data() : null; ?>

Checkout

Billing Details

Please enter your first name.
Please enter your last name.
Please enter a valid email.
Please enter your phone number.
Please enter your address.
Please enter your city.
Please enter your state.
Please enter your ZIP code.

Shipping Address

Order Notes

Payment Method

Your Order

0 ? $item['sale_price'] : $item['regular_price']; $item_total = $price * $item['quantity']; $image_url = $item['thumbnail'] ? UPLOADS_URL . 'products/' . $item['thumbnail'] : ASSETS_PATH . 'images/no-image.jpg'; ?>
<?= htmlspecialchars($item['name']) ?>
Qty: ×
Subtotal
Shipping 0 ? format_currency($shipping_cost) : 'FREE' ?>
Tax (%)
0): ?>
Discount -
Total

Your payment information is processed securely