diff --git a/assets/style.css b/assets/style.css index 8418c1e..5a66723 100644 --- a/assets/style.css +++ b/assets/style.css @@ -264,4 +264,59 @@ form button:hover { .my-container { 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; } \ No newline at end of file diff --git a/cart.php b/cart.php index 4810016..a11c349 100644 --- a/cart.php +++ b/cart.php @@ -78,7 +78,7 @@ session_start();
- +
diff --git a/checkout.php b/checkout.php new file mode 100644 index 0000000..6794db7 --- /dev/null +++ b/checkout.php @@ -0,0 +1,135 @@ +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(); +?> + + + + + + + <?php echo $success ? 'Ordine confermato' : 'Errore ordine'; ?> - TechStore + + + +
+ +
+ + + + Benvenuto, + + + +
+
+ +
+ +

Ordine confermato!

+

Il tuo ordine è stato elaborato con successo.

+

Numero ordine: #

+

Totale:

+ + +

Errore nell'ordine

+

+ + +
+ + \ No newline at end of file diff --git a/orders.php b/orders.php new file mode 100644 index 0000000..d5c626a --- /dev/null +++ b/orders.php @@ -0,0 +1,92 @@ +prepare($sql); +$stmt->bind_param("i", $_SESSION['id']); +$stmt->execute(); +$result = $stmt->get_result(); + +if (!$result) { + die("Errore nella query: " . $conn->error); +} +?> + + + + + + + Il mio account - TechStore + + + + +
+ +
+ + + + Benvenuto, + + + +
+
+ + +
+
+ +
+
+

I miei ordini

+ num_rows > 0): ?> +
+ fetch_assoc()): ?> +
+
+

Ordine #

+ + + +
+
+

Data:

+

Prodotti:

+

Totale:

+
+
+ +
+ +

Non hai ancora effettuato ordini.

+ +
+
+ + + + + close(); ?> + + diff --git a/product.php b/product.php index 6d4d764..dcda353 100644 --- a/product.php +++ b/product.php @@ -35,7 +35,7 @@ if ($stmt = $conn->prepare($sql)) {
- + Benvenuto,