mirror of
https://codeberg.org/ThisIsMiseryy/techstore
synced 2026-05-14 14:52:04 +00:00
Aggiunto controllo stock in checkout per prevenire stock negativi
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user