$quantity) { $product = db()->fetch( "SELECT product_id, name, price, sale_price, stock, images FROM products WHERE product_id = :id AND is_active = 1", ['id' => $productId] ); if ($product) { $images = json_decode($product['images'] ?? '[]', true); $product['image'] = !empty($images) ? $images[0] : '/assets/images/placeholder-product.svg'; $product['quantity'] = min($quantity, $product['stock']); $product['unit_price'] = $product['sale_price'] ?? $product['price']; $product['total'] = $product['unit_price'] * $product['quantity']; $subtotal += $product['total']; $cartItems[] = $product; } } // Get shipping settings $shippingSettings = getSetting('shipping', [ 'flat_rate_enabled' => true, 'flat_rate_amount' => 5.99, 'free_shipping_threshold' => 50 ]); $shippingCost = 0; if ($shippingSettings['flat_rate_enabled'] ?? true) { if ($subtotal >= ($shippingSettings['free_shipping_threshold'] ?? 50)) { $shippingCost = 0; } else { $shippingCost = $shippingSettings['flat_rate_amount'] ?? 5.99; } } $total = $subtotal + $shippingCost; // Get Stripe publishable key $stripeKey = STRIPE_PUBLISHABLE_KEY; $errors = []; // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Validate form $email = trim($_POST['email'] ?? ''); $name = trim($_POST['name'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $address = trim($_POST['address'] ?? ''); $city = trim($_POST['city'] ?? ''); $state = trim($_POST['state'] ?? ''); $zip = trim($_POST['zip'] ?? ''); if (empty($email)) $errors['email'] = 'Email is required'; if (empty($name)) $errors['name'] = 'Name is required'; if (empty($address)) $errors['address'] = 'Address is required'; if (empty($city)) $errors['city'] = 'City is required'; if (empty($state)) $errors['state'] = 'State is required'; if (empty($zip)) $errors['zip'] = 'ZIP code is required'; if (empty($errors)) { // Create order $orderId = generateId('ord_'); $orderNumber = generateOrderNumber(); // Get or create customer $customerId = null; if ($customer) { $customerId = $customer['customer_id']; } else { $customerId = CustomerAuth::createGuest($email, $name, $phone); } // Prepare order items $orderItems = []; foreach ($cartItems as $item) { $orderItems[] = [ 'product_id' => $item['product_id'], 'name' => $item['name'], 'price' => $item['unit_price'], 'quantity' => $item['quantity'], 'total' => $item['total'] ]; } // Insert order db()->insert('orders', [ 'order_id' => $orderId, 'order_number' => $orderNumber, 'customer_id' => $customerId, 'customer_email' => $email, 'customer_name' => $name, 'customer_phone' => $phone, 'items' => json_encode($orderItems), 'subtotal' => $subtotal, 'shipping_cost' => $shippingCost, 'total' => $total, 'shipping_address' => json_encode([ 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'country' => 'USA' ]), 'shipping_method' => 'standard', 'payment_method' => 'stripe', 'payment_status' => 'pending', 'order_status' => 'pending' ]); // Insert order items for reporting foreach ($orderItems as $item) { db()->insert('order_items', [ 'order_id' => $orderId, 'product_id' => $item['product_id'], 'name' => $item['name'], 'price' => $item['price'], 'quantity' => $item['quantity'], 'total' => $item['total'] ]); } // Reduce stock foreach ($cartItems as $item) { db()->query( "UPDATE products SET stock = stock - :qty WHERE product_id = :id", ['qty' => $item['quantity'], 'id' => $item['product_id']] ); } // Store order ID for payment $_SESSION['pending_order_id'] = $orderId; // Redirect to payment page redirect('/payment.php?order=' . $orderId); } } require_once __DIR__ . '/includes/header.php'; ?>

Checkout

Contact Information

Logged in as

Already have an account? Sign in

Shipping Address

Order Notes (Optional)

Order Summary

x

Subtotal
Shipping FREE

Total
Back to Cart

Secure checkout powered by Stripe