D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
slg.encodersunlimited.com
/
Filename :
checkout.php
back
Copy
<?php include 'config.php'; // Database connection session_start(); if (!isset($_SESSION['user_id'])) { echo "<script>alert('Please log in to continue.'); window.location.href='login.php';</script>"; exit; } $user_id = $_SESSION['user_id']; $total_price = 0; $additional_charge = 500; $gst_rate = 18; // GST percentage // Fetch cart items from the database $cart_query = "SELECT c.product_id, c.quantity, p.name, p.price, p.image_1 FROM cart c JOIN slgtoys_products p ON c.product_id = p.id WHERE c.user_id = '$user_id'"; $result = mysqli_query($conn, $cart_query); $products = []; if (mysqli_num_rows($result) == 0) { echo "<script>alert('Your cart is empty!'); window.location.href='shop.php';</script>"; exit; } while ($row = mysqli_fetch_assoc($result)) { $products[] = $row; $total_price += $row['price'] * $row['quantity']; } $gst_amount = ($total_price + $additional_charge) * ($gst_rate / 100); $grand_total = $total_price + $additional_charge + $gst_amount; if ($_SERVER['REQUEST_METHOD'] == "POST") { $payment_method = $_POST['payment_method']; // Insert order $order_query = "INSERT INTO orders (customer_id, total_price, additional_charge, gst, grand_total, payment_method) VALUES ('$user_id', '$total_price', '$additional_charge', '$gst_amount', '$grand_total', '$payment_method')"; if (mysqli_query($conn, $order_query)) { $order_id = mysqli_insert_id($conn); // Insert order items foreach ($products as $product) { $product_id = $product['product_id']; $product_price = $product['price']; $quantity = $product['quantity']; mysqli_query($conn, "INSERT INTO order_items (order_id, product_id, price, quantity) VALUES ('$order_id', '$product_id', '$product_price', '$quantity')"); } // Clear cart after successful order mysqli_query($conn, "DELETE FROM cart WHERE user_id = '$user_id'"); // Redirect to account page echo "<script>alert('Order placed successfully!'); window.location='my_account.php?user_id=$user_id';</script>"; } else { echo "Error: " . mysqli_error($conn); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Checkout</title> <script src="https://cdn.tailwindcss.com"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" /> </head> <body class="bg-gray-100"> <div class="container mx-auto px-4 py-8"> <h1 class="text-4xl font-bold text-center text-blue-600 mb-8">Checkout</h1> <div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> <div class="bg-white p-6 shadow-lg rounded-lg"> <h2 class="text-2xl font-semibold text-gray-800 mb-4">Order Summary</h2> <div class="space-y-4"> <?php foreach ($products as $product) { ?> <div class="flex items-center border-b pb-4"> <img src="slgadmintoys/<?php echo $product['image_1']; ?>" class="w-20 h-20 object-cover rounded-md mr-4"> <div> <h3 class="text-lg font-semibold text-gray-800"><?php echo $product['name']; ?></h3> <p class="text-sm text-gray-600">₹<?php echo number_format($product['price'], 2); ?> x <?php echo $product['quantity']; ?></p> </div> <p class="ml-auto font-bold text-gray-800">₹<?php echo number_format($product['price'] * $product['quantity'], 2); ?></p> </div> <?php } ?> </div> <div class="mt-6 border-t pt-4"> <p class="flex justify-between text-gray-700"><span>Subtotal:</span> <span>₹<?php echo number_format($total_price, 2); ?></span></p> <p class="flex justify-between text-gray-700"><span>Additional Charge:</span> <span>₹<?php echo number_format($additional_charge, 2); ?></span></p> <p class="flex justify-between text-gray-700"><span>GST (<?php echo $gst_rate; ?>%):</span> <span>₹<?php echo number_format($gst_amount, 2); ?></span></p> <p class="flex justify-between text-xl font-bold text-gray-900 mt-4"><span>Total:</span> <span>₹<?php echo number_format($grand_total, 2); ?></span></p> </div> </div> <div class="bg-white p-6 shadow-lg rounded-lg"> <h2 class="text-2xl font-semibold text-gray-800 mb-4">Payment Method</h2> <form method="POST" class="space-y-4"> <label for="payment_method" class="block text-gray-600 text-sm font-medium">Choose Payment Method:</label> <select name="payment_method" required class="w-full p-2 border rounded-lg focus:ring-2 focus:ring-blue-500"> <option value="cash">Cash on Delivery</option> </select> <button type="submit" class="bg-yellow-500 text-white font-bold py-3 px-6 rounded-lg w-full hover:bg-yellow-600 transition">Place Order</button> </form> </div> </div> </div> </body> </html>