File size: 2,161 Bytes
42c6a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
```php
<?php
require_once 'config.php';

// Get cart from session or cookie
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : (isset($_COOKIE['cart']) ? json_decode($_COOKIE['cart'], true) : []);

// Calculate total
$total = 0;
foreach($cart as $item) {
    $total += $item['price'] * $item['quantity'];
}

// Handle form submission
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $customer = [
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'address' => $_POST['address'],
        'phone' => $_POST['phone']
    ];
    
    // Prepare order data
    $order_data = [
        'customer' => $customer,
        'items' => $cart,
        'total' => $total + 5 // Including shipping
    ];
    
    // Create order (this would be a call to your orders.php API in a real app)
    $order_id = createOrder($order_data['customer'], $order_data['total'], $cart);
    
    // Clear cart
    unset($_SESSION['cart']);
    setcookie('cart', '', time() - 3600, '/');
    
    // Redirect to thank you page
    header('Location: thank_you.php?order_id=' . $order_id);
    exit;
}

// Function to create order (simplified for example)
function createOrder($customer, $total, $items) {
    // In a real app, this would call your orders.php API endpoint
    $db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
    
    // Insert order
    $stmt = $db->prepare("INSERT INTO orders (customer_name, customer_email, customer_address, total_amount) VALUES (?, ?, ?, ?)");
    $stmt->execute([$customer['name'], $customer['email'], $customer['address'], $total]);
    $order_id = $db->lastInsertId();
    
    // Insert order items
    foreach($items as $item) {
        $stmt = $db->prepare("INSERT INTO order_items (order_id, product_id, product_name, quantity, unit_price) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$order_id, $item['id'], $item['name'], $item['quantity'], $item['price']]);
        
        // Update product stock
        $stmt = $db->prepare("UPDATE products SET stock = stock - ? WHERE id = ?");
        $stmt->execute([$item['quantity'], $item['id']]);
    }
    
    return $order_id;
}
?>
```