Implementazione ordini (estremamente vibe-coded)

This commit is contained in:
2026-04-08 11:14:45 +02:00
parent 18a7ca1bab
commit a5230401a3
2 changed files with 136 additions and 1 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ session_start();
<div style="text-align: right; margin-top: 20px;"> <div style="text-align: right; margin-top: 20px;">
<button class="clear-cart-btn" onclick="clearCart()">Svuota carrello</button> <button class="clear-cart-btn" onclick="clearCart()">Svuota carrello</button>
<button class="checkout-btn">Procedi all'acquisto</button> <a href="order.php"><button class="checkout-btn">Procedi all'acquisto</button></a>
</div> </div>
<?php endif; ?> <?php endif; ?>
</div> </div>
+135
View File
@@ -0,0 +1,135 @@
<?php
session_start();
include 'db_conf.php';
// Controlla se l'utente è loggato
if (!isset($_SESSION['id'])) {
header('Location: login.php');
exit();
}
// Leggi il carrello dal cookie
$cart = [];
$cartCookie = isset($_COOKIE['cart']) ? $_COOKIE['cart'] : '';
if ($cartCookie) {
$cart = json_decode(urldecode($cartCookie), true);
}
// Se il carrello è vuoto, reindirizza al carrello
if (empty($cart)) {
header('Location: cart.php');
exit();
}
$user_id = $_SESSION['id'];
$order_total = 0;
$error = '';
// Inzia la transazione
$conn->begin_transaction();
try {
// Calcola il totale dell'ordine
foreach ($cart as $item) {
$order_total += $item['price'] * $item['quantity'];
}
// Crea un nuovo ordine
$sql_order = "INSERT INTO Orders (UserID, Total, Status) VALUES (?, ?, 'pending')";
$stmt_order = $conn->prepare($sql_order);
if (!$stmt_order) {
throw new Exception("Errore nella preparazione della query: " . $conn->error);
}
$stmt_order->bind_param("id", $user_id, $order_total);
$stmt_order->execute();
$order_id = $conn->insert_id;
// Per ogni prodotto nel carrello
foreach ($cart as $item) {
$product_id = $item['id'];
$quantity = $item['quantity'];
$price = $item['price'];
// Aggiungi item all'ordine
$sql_item = "INSERT INTO OrderItems (OrderID, ProductID, Quantity, Price) VALUES (?, ?, ?, ?)";
$stmt_item = $conn->prepare($sql_item);
if (!$stmt_item) {
throw new Exception("Errore nella preparazione della query: " . $conn->error);
}
$stmt_item->bind_param("iiii", $order_id, $product_id, $quantity, $price);
$stmt_item->execute();
// Riduci lo stock del prodotto
$sql_stock = "UPDATE Products SET StockQuantity = StockQuantity - ? WHERE ProductID = ?";
$stmt_stock = $conn->prepare($sql_stock);
if (!$stmt_stock) {
throw new Exception("Errore nella preparazione della query: " . $conn->error);
}
$stmt_stock->bind_param("ii", $quantity, $product_id);
$stmt_stock->execute();
$stmt_item->close();
$stmt_stock->close();
}
// Conferma la transazione
$conn->commit();
// Svuota il carrello
setcookie('cart', '', time() - 3600, '/');
// Mostra pagina di successo
$success = true;
} catch (Exception $e) {
// Annulla la transazione in caso di errore
$conn->rollback();
$error = "Errore nell'elaborazione dell'ordine: " . $e->getMessage();
$success = false;
}
$stmt_order->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $success ? 'Ordine confermato' : 'Errore ordine'; ?> - TechStore</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;">
<div id="logo" style="font-size: 24px; font-weight: bold;"><a href="index.php">TechStore</a></div>
<div>
<a href="cart.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Carrello</button></a>
<?php if (isset($_SESSION['name'])): ?>
<a href="logout.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #dc3545; color: white; border: none; border-radius: 4px; cursor: pointer;">Logout</button></a>
<span style="margin-left: 10px;">Benvenuto, <?php echo htmlspecialchars($_SESSION['name']); ?></span>
<?php else: ?>
<a href="login.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer;">Accesso</button></a>
<?php endif; ?>
</div>
</header>
<div style="max-width: 600px; margin: 50px auto; padding: 20px; text-align: center; border: 1px solid #ddd; border-radius: 8px; background-color: #fff;">
<?php if ($success): ?>
<h1 style="color: #28a745;">Ordine confermato!</h1>
<p>Il tuo ordine è stato elaborato con successo.</p>
<p><strong>Numero ordine:</strong> #<?php echo $order_id; ?></p>
<p><strong>Totale:</strong> €<?php echo number_format($order_total, 2, ',', '.'); ?></p>
<a href="index.php"><button style="padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px;">Torna alla home</button></a>
<?php else: ?>
<h1 style="color: #dc3545;">Errore nell'ordine</h1>
<p><?php echo htmlspecialchars($error); ?></p>
<a href="cart.php"><button style="padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px;">Torna al carrello</button></a>
<?php endif; ?>
</div>
</body>
</html>