Файловый менеджер - Редактировать - /home/skymarketplace/public_html/cart_action.php
Назад
<?php session_start(); include 'admin/include/auth.php'; // adjust this path as needed $cuser = new auth(); header('Content-Type: application/json'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_POST['Mode'] == 'update_qty') { $user_id = $_SESSION['uid']; $product_id = $_POST['product_id']; $new_qty = $_POST['new_qty']; // Validate input if (!is_numeric($new_qty) || $new_qty < 1) { echo json_encode(['success' => false, 'message' => 'Invalid quantity']); exit; } $result = $cuser->update_cart_qty($user_id, $product_id, $new_qty); echo json_encode(['success' => $result]); exit; } if ($_POST['Mode'] == 'remove_item') { $user_id = $_SESSION['uid']; $product_id = $_POST['product_id']; $result = $cuser->remove_cart_item($user_id, $product_id); echo json_encode(['success' => $result]); exit; } } echo json_encode(['success' => false, 'message' => 'Invalid request']); if ($_POST['Mode'] == 'get_cart_total') { $user_id = $_SESSION['uid']; $stmt = $cuser->conn->prepare("SELECT SUM(p_qty * product.price) AS total FROM cart JOIN product ON cart.product_id = product.id WHERE cart.user_id = ?"); $stmt->execute([$user_id]); $total = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0; echo json_encode(['success' => true, 'total' => number_format($total)]); exit; } if ($_POST['Mode'] == 'update_qty') { $product_id = $_POST['product_id']; $new_qty = $_POST['new_qty']; $user_id = $_SESSION['uid']; $stmt = $cuser->conn->prepare("UPDATE cart SET p_qty = ? WHERE user_id = ? AND product_id = ?"); if ($stmt->execute([$new_qty, $user_id, $product_id])) { // Get price of product $price_stmt = $cuser->conn->prepare("SELECT price FROM product WHERE id = ?"); $price_stmt->execute([$product_id]); $price = $price_stmt->fetchColumn(); // New subtotal for that product $new_subtotal = $price * $new_qty; // Get updated total $total_stmt = $cuser->conn->prepare("SELECT SUM(p_qty * product.price) AS total FROM cart JOIN product ON cart.product_id = product.id WHERE cart.user_id = ?"); $total_stmt->execute([$user_id]); $cart_total = $total_stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0; echo json_encode([ 'success' => true, 'new_subtotal' => $new_subtotal, 'cart_total' => number_format($cart_total) ]); } else { echo json_encode(['success' => false]); } exit; } if ($_POST['Mode'] === 'order_place') { session_start(); require_once 'admin/include/auth.php'; $cuser = new auth(); $user_id = $_SESSION['uid']; $payment = $_POST['payment'] ?? null; $terms = isset($_POST['terms']); $delivery_location_id = isset($_POST['delivery_location_id']) ? intval($_POST['delivery_location_id']) : null; $order_note = $_POST['order_note'] ?? ''; $total_price = $_POST['total_price'] ?? 0; if (!$payment || !$terms || !$delivery_location_id) { echo json_encode(['status' => 'error', 'message' => 'Missing required fields!']); exit(); } // Prevent duplicate orders $checkRecent = $cuser->checkDuplicateOrder($user_id); if ($checkRecent) { echo json_encode(['status' => 'error', 'message' => 'Duplicate order detected. Please wait a few minutes.']); exit(); } // Fetch cart items $cart = $cuser->checkOut_product($user_id); if (!$cart || count($cart) === 0) { echo json_encode(['status' => 'error', 'message' => 'Your cart is empty.']); exit(); } // Use placeholder shipping address $shipping_address = 'Delivery Location Selected'; // Place order (assuming your method supports all these) $order_id = $cuser->order_place( $user_id, $shipping_address, $total_price, $payment, $delivery_location_id, $order_note ); if ($order_id) { $cuser->clearUserCart($user_id); echo json_encode(['status' => 'success', 'message' => 'Your order was placed successfully!']); } else { echo json_encode(['status' => 'error', 'message' => 'Failed to place order. Try again.']); } exit(); }
| ver. 1.4 |
Github
|
.
| PHP 8.1.31 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка