-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddToCart.php
98 lines (82 loc) · 3.78 KB
/
AddToCart.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include "conx.php"; // Include your database connection
$data = json_decode(file_get_contents("php://input"), true);
// Debug: Log incoming data
file_put_contents("debug.log", print_r($data, true));
// Validate input
if (!empty($data['user_id']) && !empty($data['id']) && !empty($data['price']) && !empty($data['quantity']) && !empty($data['image'])) {
$user_id = $data['user_id'];
$product_id = $data['id'];
$price = $data['price'];
$quantity = $data['quantity'];
$image = $data['image'];
// Check if quantity is greater than available stock
$stockQuery = "SELECT quantity FROM products WHERE id = ?";
$stmt = $conn->prepare($stockQuery);
$stmt->bind_param("i", $product_id);
$stmt->execute();
$result = $stmt->get_result();
$product = $result->fetch_assoc();
if ($product) {
$availableStock = $product['quantity'];
// Check if the quantity to add exceeds available stock
if ($quantity > $availableStock) {
echo json_encode(["success" => false, "message" => "Cannot add more than {$availableStock} items to the cart."]);
$stmt->close();
$conn->close();
exit();
}
} else {
echo json_encode(["success" => false, "message" => "Product not found."]);
$stmt->close();
$conn->close();
exit();
}
$totalPrice = $price * $quantity;
// Check if the product already exists in the cart
$checkQuery = "SELECT * FROM cart WHERE user_id = ? AND product = ?";
$stmt = $conn->prepare($checkQuery);
$stmt->bind_param("ii", $user_id, $product_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Update existing cart item
$existingProduct = $result->fetch_assoc();
$newQuantity = $existingProduct['quantity'] + $quantity;
// Check again if the updated quantity exceeds available stock
if ($newQuantity > $availableStock) {
echo json_encode(["success" => false, "message" => "Cannot update the cart. The new quantity exceeds available stock."]);
$stmt->close();
$conn->close();
exit();
}
$newTotalPrice = $price * $newQuantity;
$updateQuery = "UPDATE cart SET quantity = ?, totalPrice = ?, image = ? WHERE user_id = ? AND product = ?";
$stmt = $conn->prepare($updateQuery);
$stmt->bind_param("idisi", $newQuantity, $newTotalPrice, $image, $user_id, $product_id);
if ($stmt->execute()) {
echo json_encode(["success" => true, "message" => "Product quantity updated in cart"]);
} else {
echo json_encode(["success" => false, "message" => "Failed to update product in cart"]);
}
} else {
// Insert new cart item
$insertQuery = "INSERT INTO cart (user_id, product, price, quantity, totalPrice, image) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param("iidids", $user_id, $product_id, $price, $quantity, $totalPrice, $image);
if ($stmt->execute()) {
echo json_encode(["success" => true, "message" => "Product added to cart successfully"]);
} else {
echo json_encode(["success" => false, "message" => "Failed to add product to cart"]);
}
}
$stmt->close();
} else {
echo json_encode(["success" => false, "message" => "Invalid product data"]);
}
$conn->close();
?>