���� JFIF �� � ( %"1"%)+...383,7(-.-
![]() Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20 System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 User : apache ( 48) PHP Version : 7.4.20 Disable Function : NONE Directory : /proc/self/root/var/www/html/tien-dien/code/checkout/ |
<?php global $DB, $INFO, $print, $v_lang, $CORE; // Initialize session if not already started if (session_status() == PHP_SESSION_NONE) { session_start(); } // Check if cart is empty if (!isset($_SESSION['cart']) || empty($_SESSION['cart'])) { $print->refresh('?act=cart'); exit(); } // Handle buy now action $action = isset($_GET['action']) ? $_GET['action'] : ''; $product_id = isset($_GET['id']) ? intval($_GET['id']) : 0; if ($action == 'buy_now' && $product_id > 0) { // Clear cart first $_SESSION['cart'] = array(); // Get product details $product = $DB->fetch_row($DB->query("SELECT * FROM tb_product WHERE id='$product_id' AND status=0")); if ($product && $product['price'] > 0) { // Add product to cart $price = getDiscountedPrice($product); $_SESSION['cart'][] = array( 'id' => $product_id, 'title' => $product['title'], 'price' => $price, 'quantity' => 1, 'path_img' => $product['path_img'] ); } } // Get available shipping methods $shipping_methods = array(); $query = "SELECT * FROM tb_shipping_methods WHERE status = 1 ORDER BY sort_order ASC"; $result = $DB->query($query); while ($row = $DB->fetch_row($result)) { $shipping_methods[] = $row; } // Calculate cart totals $subtotal = 0; $total_items = 0; foreach ($_SESSION['cart'] as $item) { $subtotal += $item['price'] * $item['quantity']; $total_items += $item['quantity']; } // Set default shipping method and cost $shipping_method_id = isset($_SESSION['shipping_method_id']) ? $_SESSION['shipping_method_id'] : 0; $shipping_cost = 0; // If shipping method is selected, get its cost if ($shipping_method_id > 0) { foreach ($shipping_methods as $method) { if ($method['id'] == $shipping_method_id) { $shipping_cost = $method['cost']; break; } } } elseif (!empty($shipping_methods)) { // Set first shipping method as default $shipping_method_id = $shipping_methods[0]['id']; $shipping_cost = $shipping_methods[0]['cost']; $_SESSION['shipping_method_id'] = $shipping_method_id; } // Apply free shipping if subtotal is over 500,000 VND if ($subtotal >= 500000) { foreach ($shipping_methods as $method) { if ($method['name'] == 'Giao hàng miễn phí') { $shipping_method_id = $method['id']; $shipping_cost = 0; $_SESSION['shipping_method_id'] = $shipping_method_id; break; } } } $total = $subtotal + $shipping_cost; // Add shipping cost to total // Process order submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_order'])) { // Validate form data $errors = array(); // Required fields $required_fields = array('fullname', 'email', 'phone', 'address', 'city'); foreach ($required_fields as $field) { if (empty($_POST[$field])) { $errors[] = 'Vui lòng nhập ' . $field; } } if (empty($errors)) { // Get shipping method from form $shipping_method_id = isset($_POST['shipping_method_id']) ? intval($_POST['shipping_method_id']) : $shipping_method_id; // Get shipping cost from selected method $shipping_cost = 0; foreach ($shipping_methods as $method) { if ($method['id'] == $shipping_method_id) { $shipping_cost = $method['cost']; break; } } // Apply free shipping if subtotal is over 500,000 VND if ($subtotal >= 500000) { foreach ($shipping_methods as $method) { if ($method['name'] == 'Giao hàng miễn phí') { $shipping_method_id = $method['id']; $shipping_cost = 0; break; } } } // Calculate order total with shipping $total = $subtotal + $shipping_cost; // Insert order into database $fullname = $DB->real_escape_string($_POST['fullname']); $email = $DB->real_escape_string($_POST['email']); $phone = $DB->real_escape_string($_POST['phone']); $address = $DB->real_escape_string($_POST['address']); $city = $DB->real_escape_string($_POST['city']); $notes = $DB->real_escape_string($_POST['notes']); $payment_method = $DB->real_escape_string($_POST['payment_method']); $shipping_method_id = isset($_POST['shipping_method_id']) ? intval($_POST['shipping_method_id']) : $shipping_method_id; $shipping_cost = 0; // Get shipping cost from selected method if ($shipping_method_id > 0) { foreach ($shipping_methods as $method) { if ($method['id'] == $shipping_method_id) { $shipping_cost = $method['cost']; break; } } } // Apply free shipping if subtotal is over 500,000 VND if ($subtotal >= 500000) { foreach ($shipping_methods as $method) { if ($method['name'] == 'Giao hàng miễn phí') { $shipping_method_id = $method['id']; $shipping_cost = 0; break; } } } $order_date = date('Y-m-d H:i:s'); $status = 0; // Pending $total = $subtotal + $shipping_cost; // Recalculate total with shipping $DB->query("INSERT INTO tb_orders (fullname, email, phone, address, city, notes, payment_method, shipping_method_id, shipping_cost, order_date, total, status) VALUES ('$fullname', '$email', '$phone', '$address', '$city', '$notes', '$payment_method', '$shipping_method_id', '$shipping_cost', '$order_date', '$total', '$status')"); $order_id = $DB->insert_id(); // Insert order items foreach ($_SESSION['cart'] as $item) { $product_id = $item['id']; $price = $item['price']; $quantity = $item['quantity']; $DB->query("INSERT INTO tb_order_items (order_id, product_id, price, quantity) VALUES ('$order_id', '$product_id', '$price', '$quantity')"); } // Clear cart $_SESSION['cart'] = array(); // Redirect to thank you page $print->refresh('?act=checkout&action=thank_you&order_id=' . $order_id); exit(); } } // Get available shipping methods $shipping_methods = array(); $query = "SELECT * FROM tb_shipping_methods WHERE status = 1 ORDER BY sort_order ASC"; $result = $DB->query($query); while ($row = $DB->fetch_row($result)) { $shipping_methods[] = $row; } // Calculate cart totals $subtotal = 0; $total_items = 0; foreach ($_SESSION['cart'] as $item) { $subtotal += $item['price'] * $item['quantity']; $total_items += $item['quantity']; } // Set default shipping method and cost $shipping_method_id = isset($_SESSION['shipping_method_id']) ? $_SESSION['shipping_method_id'] : 0; $shipping_cost = 0; // If shipping method is selected, get its cost if ($shipping_method_id > 0) { foreach ($shipping_methods as $method) { if ($method['id'] == $shipping_method_id) { $shipping_cost = $method['cost']; break; } } } elseif (!empty($shipping_methods)) { // Set first shipping method as default $shipping_method_id = $shipping_methods[0]['id']; $shipping_cost = $shipping_methods[0]['cost']; $_SESSION['shipping_method_id'] = $shipping_method_id; } // Apply free shipping if subtotal is over 500,000 VND if ($subtotal >= 500000) { foreach ($shipping_methods as $method) { if ($method['name'] == 'Giao hàng miễn phí') { $shipping_method_id = $method['id']; $shipping_cost = 0; $_SESSION['shipping_method_id'] = $shipping_method_id; break; } } } $total = $subtotal + $shipping_cost; // Add shipping cost to total // Display thank you page if order completed if ($action == 'thank_you') { $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0; $CORE->title_page = 'Đặt hàng thành công | '; include 'layout/'.$INFO['path_skin'].'/header.php'; include 'layout/'.$INFO['path_skin'].'/menu.php'; ?> <div class="container mt-4 mb-4"> <div class="row"> <div class="col-md-12"> <div class="card"> <div class="card-body text-center"> <h1 class="text-success"><i class="fa fa-check-circle"></i></h1> <h2>Đặt hàng thành công!</h2> <p>Cảm ơn bạn đã đặt hàng. Mã đơn hàng của bạn là: <strong>#<?php echo $order_id; ?></strong></p> <p>Chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất.</p> <a href="?act=product" class="btn btn-primary mt-3">Tiếp tục mua sắm</a> </div> </div> </div> </div> </div> <?php include 'layout/'.$INFO['path_skin'].'/footer.php'; exit(); } $CORE->title_page = 'Thanh toán | '; include 'layout/'.$INFO['path_skin'].'/header.php'; include 'layout/'.$INFO['path_skin'].'/menu.php'; ?> <div class="container mt-4 mb-4"> <div class="row"> <div class="col-md-12"> <h1 class="page-title">Thanh toán</h1> </div> </div> <?php if (!empty($errors)): ?> <div class="row"> <div class="col-md-12"> <div class="alert alert-danger"> <ul> <?php foreach ($errors as $error): ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </ul> </div> </div> </div> <?php endif; ?> <div class="row"> <div class="col-md-8"> <div class="card mb-4"> <div class="card-header"> <h5>Thông tin thanh toán</h5> </div> <div class="card-body"> <form method="post" action=""> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="fullname">Họ và tên *</label> <input type="text" class="form-control" id="fullname" name="fullname" required> </div> </div> </div> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label for="email">Email *</label> <input type="email" class="form-control" id="email" name="email" required> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="phone">Số điện thoại *</label> <input type="text" class="form-control" id="phone" name="phone" required> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="address">Địa chỉ *</label> <input type="text" class="form-control" id="address" name="address" required> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="city">Thành phố *</label> <input type="text" class="form-control" id="city" name="city" required> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="notes">Ghi chú đơn hàng (tùy chọn)</label> <textarea class="form-control" id="notes" name="notes" rows="3"></textarea> </div> </div> </div> </div> </div> </div> <div class="col-md-4"> <div class="card"> <div class="card-header"> <h5>Đơn hàng của bạn</h5> </div> <div class="card-body"> <table class="table"> <thead> <tr> <th>Sản phẩm</th> <th class="text-right">Tổng</th> </tr> </thead> <tbody> <?php foreach ($_SESSION['cart'] as $item): ?> <tr> <td><?php echo $item['title']; ?> <strong>× <?php echo $item['quantity']; ?></strong></td> <td class="text-right"><?php echo number_format($item['price'] * $item['quantity']); ?> VNĐ</td> </tr> <?php endforeach; ?> </tbody> <tfoot> <tr> <th>Tạm tính</th> <th class="text-right"><?php echo number_format($subtotal); ?> VNĐ</th> </tr> <tr> <th>Phí vận chuyển</th> <th class="text-right"> <?php if ($shipping_cost > 0): ?> <?php echo number_format($shipping_cost); ?> VNĐ <?php else: ?> Miễn phí <?php endif; ?> </th> </tr> <tr> <th>Tổng cộng</th> <th class="text-right"><?php echo number_format($total); ?> VNĐ</th> </tr> </tfoot> </table> </div> <div class="card-footer"> <div class="shipping-method mb-4"> <h6>Phương thức vận chuyển</h6> <?php foreach ($shipping_methods as $method): ?> <div class="form-group"> <div class="custom-control custom-radio"> <input type="radio" id="shipping_<?php echo $method['id']; ?>" name="shipping_method_id" class="custom-control-input" value="<?php echo $method['id']; ?>" <?php echo ($shipping_method_id == $method['id']) ? 'checked' : ''; ?>> <label class="custom-control-label" for="shipping_<?php echo $method['id']; ?>"> <?php echo $method['name']; ?> <?php if ($method['cost'] > 0): ?> - <?php echo number_format($method['cost']); ?> VNĐ <?php else: ?> - Miễn phí <?php endif; ?> </label> <?php if (!empty($method['description'])): ?> <small class="form-text text-muted"><?php echo $method['description']; ?></small> <?php endif; ?> </div> </div> <?php endforeach; ?> </div> <div class="payment-method"> <h6>Phương thức thanh toán</h6> <div class="form-group"> <div class="custom-control custom-radio"> <input type="radio" id="payment_cod" name="payment_method" class="custom-control-input" value="cod" checked> <label class="custom-control-label" for="payment_cod">Thanh toán khi nhận hàng</label> </div> </div> <div class="form-group"> <div class="custom-control custom-radio"> <input type="radio" id="payment_bank" name="payment_method" class="custom-control-input" value="bank"> <label class="custom-control-label" for="payment_bank">Chuyển khoản ngân hàng</label> </div> </div> </div> <p class="small text-muted">Thông tin cá nhân của bạn sẽ được sử dụng để xử lý đơn hàng và hỗ trợ trải nghiệm của bạn trên trang web này.</p> <button type="submit" name="submit_order" class="btn btn-primary btn-block">Đặt hàng</button> </form> </div> </div> </div> </div> </div> <?php include 'layout/'.$INFO['path_skin'].'/footer.php'; ?>