-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingCart.php
52 lines (41 loc) · 1.42 KB
/
ShoppingCart.php
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
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
include 'Conx.php';
if (isset($_GET['user_id'])) {
$userId = $_GET['user_id'];
// Fetch cart items with product details, including product quantity
$query = "
SELECT
cart.id,
cart.product AS product_id,
products.name AS product_name,
cart.price,
cart.quantity,
cart.totalPrice,
products.image,
products.quantity AS product_quantity
FROM cart
JOIN products ON cart.product = products.id
WHERE cart.user_id = ? AND products.status != 1"; // Exclude products with status 1
$stmt = $conn->prepare($query);
if ($stmt === false) {
die("Error in preparing the query: " . $conn->error);
}
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
if ($result === false) {
die("Error in executing the query: " . $stmt->error);
}
$cartItems = [];
while ($row = $result->fetch_assoc()) {
$cartItems[] = $row; // Add the result to the array
}
echo json_encode($cartItems); // Return the cart items with product details
} else {
echo json_encode(['error' => 'User ID is required']);
}
$conn->close();
?>