Aggiunto controllo stock in checkout per prevenire stock negativi

This commit is contained in:
2026-04-15 10:29:31 +02:00
parent 63f6c21e02
commit b252e86ced
+16
View File
@@ -52,6 +52,22 @@ try {
$quantity = $item['quantity']; $quantity = $item['quantity'];
$price = $item['price']; $price = $item['price'];
// Controlla lo stock disponibile
$sql_check_stock = "SELECT StockQuantity, ProductName FROM Products WHERE ProductID = ?";
$stmt_check = $conn->prepare($sql_check_stock);
if (!$stmt_check) {
throw new Exception("Errore nella preparazione della query di controllo stock: " . $conn->error);
}
$stmt_check->bind_param("i", $product_id);
$stmt_check->execute();
$result_check = $stmt_check->get_result();
$product = $result_check->fetch_assoc();
$stmt_check->close();
if (!$product || $product['StockQuantity'] < $quantity) {
throw new Exception("Quantità insufficiente per il prodotto '" . $product['ProductName'] . "'. Disponibile: " . ($product ? $product['StockQuantity'] : 0));
}
// Aggiungi item all'ordine // Aggiungi item all'ordine
$sql_item = "INSERT INTO OrderItems (OrderID, ProductID, Quantity, Price) VALUES (?, ?, ?, ?)"; $sql_item = "INSERT INTO OrderItems (OrderID, ProductID, Quantity, Price) VALUES (?, ?, ?, ?)";
$stmt_item = $conn->prepare($sql_item); $stmt_item = $conn->prepare($sql_item);