Merge implementazione ordini

This commit is contained in:
2026-04-08 11:28:29 +02:00
5 changed files with 284 additions and 2 deletions
+55
View File
@@ -264,4 +264,59 @@ form button:hover {
.my-container { .my-container {
flex: 1 1 100%; flex: 1 1 100%;
} }
}
/* Stili per la pagina ordini */
.orders-list {
display: flex;
flex-direction: column;
gap: 20px;
}
.order-card {
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.order-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.order-header h3 {
margin: 0;
color: #333;
}
.order-status {
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
text-transform: uppercase;
}
.status-pending {
background-color: #ffc107;
color: #212529;
}
.status-completed {
background-color: #28a745;
color: white;
}
.status-cancelled {
background-color: #dc3545;
color: white;
}
.order-details p {
margin: 5px 0;
color: #666;
} }
+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="checkout.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"><img src="img/logo.png" alt="Logo TechStore" height="32px"></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, <a href="account.php"><?php echo htmlspecialchars($_SESSION['name']); ?></a></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>
+92
View File
@@ -0,0 +1,92 @@
<?php
session_start();
include 'db_conf.php';
// Controlla se l'utente è loggato
if (!isset($_SESSION['id'])) {
header('Location: login.php');
exit();
}
// Query per ottenere gli ordini dell'utente con i prodotti
$sql = "SELECT o.OrderID, o.OrderDate, o.Total, o.Status,
GROUP_CONCAT(p.ProductName SEPARATOR ', ') AS Products
FROM Orders o
LEFT JOIN OrderItems oi ON o.OrderID = oi.OrderID
LEFT JOIN Products p ON oi.ProductID = p.ProductID
WHERE o.UserID = ?
GROUP BY o.OrderID
ORDER BY o.OrderDate DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $_SESSION['id']);
$stmt->execute();
$result = $stmt->get_result();
if (!$result) {
die("Errore nella query: " . $conn->error);
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Il mio account - TechStore</title>
<link rel="stylesheet" href="assets/style.css">
<script src="assets/js/cart.js"></script>
</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"><img src="img/logo.png" alt="Logo TechStore" height="32px"></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>
<?php if (isset($_SESSION['name'])): ?>
<div class="account-layout">
<div class="my-sidebar">
<ul>
<li><a href="account.php">Il mio account</a></li>
<li><a href="orders.php">I miei ordini</a></li>
</ul>
</div>
<div class="my-container">
<h1>I miei ordini</h1>
<?php if ($result->num_rows > 0): ?>
<div class="orders-list">
<?php while ($order = $result->fetch_assoc()): ?>
<div class="order-card">
<div class="order-header">
<h3>Ordine #<?php echo htmlspecialchars($order['OrderID']); ?></h3>
<span class="order-status status-<?php echo htmlspecialchars($order['Status']); ?>">
<?php echo htmlspecialchars(ucfirst($order['Status'])); ?>
</span>
</div>
<div class="order-details">
<p><strong>Data:</strong> <?php echo date('d/m/Y H:i', strtotime($order['OrderDate'])); ?></p>
<p><strong>Prodotti:</strong> <?php echo htmlspecialchars($order['Products']); ?></p>
<p><strong>Totale:</strong> €<?php echo number_format($order['Total'], 2, ',', '.'); ?></p>
</div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<p>Non hai ancora effettuato ordini.</p>
<?php endif; ?>
</div>
</div>
<?php else: ?>
<?php header('Location: login.php'); ?>
<?php endif; ?>
<?php $conn->close(); ?>
</body>
</html>
+1 -1
View File
@@ -35,7 +35,7 @@ if ($stmt = $conn->prepare($sql)) {
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;"> <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"><img src="img/logo.png" alt="Logo TechStore" height="32px"></a></div> <div id="logo" style="font-size: 24px; font-weight: bold;"><a href="index.php"><img src="img/logo.png" alt="Logo TechStore" height="32px"></a></div>
<div> <div>
<a href=""><button style="margin-left: 10px; padding: 8px 16px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Carrello</button></a> <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'])): ?> <?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> <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, <a href="account.php"><?php echo htmlspecialchars($_SESSION['name']); ?></a></span> <span style="margin-left: 10px;">Benvenuto, <a href="account.php"><?php echo htmlspecialchars($_SESSION['name']); ?></a></span>