added shop example with variation

This commit is contained in:
2025-04-11 21:51:53 +02:00
parent f8ad1c2210
commit 296d7dbb2a
7 changed files with 1184 additions and 200 deletions
+149
View File
@@ -0,0 +1,149 @@
<?php
include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
$product_id = $_GET['product_id'] ?? 0;
// Recupera i dettagli del prodotto
$stmt = $pdo->prepare("
SELECT p.id, p.name AS product_name,
c.name AS class_name, c.description AS class_description, c.photo AS class_photo
FROM products p
LEFT JOIN product_class_types pct ON p.id = pct.product_id AND pct.variation_id IS NULL
LEFT JOIN class_types ct ON pct.class_type_id = ct.id
LEFT JOIN classes c ON ct.class_id = c.id
WHERE p.id = ?
GROUP BY p.id
");
$stmt->execute([$product_id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$product) {
die("Prodotto non trovato.");
}
// Recupera le variazioni del prodotto
$stmt = $pdo->prepare("
SELECT id, name, price
FROM product_variations
WHERE product_id = ? AND status = 'active'
");
$stmt->execute([$product_id]);
$variations = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Recupera le variazioni delle classi associate al prodotto
$stmt = $pdo->prepare("
SELECT ct.id, ct.level, ct.day_of_week
FROM product_class_types pct
JOIN class_types ct ON pct.class_type_id = ct.id
WHERE pct.product_id = ? AND pct.variation_id IS NULL
");
$stmt->execute([$product_id]);
$class_types = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dettaglio Prodotto - <?php echo htmlspecialchars($product['product_name']); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.product-image {
width: 100%;
height: 300px;
object-fit: cover;
border-radius: 8px;
}
.price-display {
font-size: 1.5rem;
color: #28a745;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4"><?php echo htmlspecialchars($product['product_name']); ?></h1>
<div class="row">
<div class="col-md-6">
<img src="<?php echo htmlspecialchars($product['class_photo'] ?: 'default_class_image.jpg'); ?>" alt="<?php echo htmlspecialchars($product['class_name']); ?>" class="product-image">
</div>
<div class="col-md-6">
<h3><?php echo htmlspecialchars($product['class_name']); ?></h3>
<p><?php echo htmlspecialchars($product['class_description']); ?></p>
<div class="mb-3">
<label for="variation_select" class="form-label">Seleziona Variazione Prodotto:</label>
<select id="variation_select" class="form-select">
<option value="">-- Seleziona --</option>
<?php foreach ($variations as $variation): ?>
<option value="<?php echo $variation['id']; ?>" data-price="<?php echo $variation['price']; ?>">
<?php echo htmlspecialchars($variation['name']); ?> (<?php echo $variation['price']; ?>€)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="class_type_select" class="form-label">Seleziona Variazione Classe:</label>
<select id="class_type_select" class="form-select">
<option value="">-- Seleziona --</option>
<?php foreach ($class_types as $class_type): ?>
<option value="<?php echo $class_type['id']; ?>">
<?php echo htmlspecialchars($class_type['level'] . ' (' . $class_type['day_of_week'] . ')'); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="price-display" id="price_display">Prezzo: -- </div>
<button id="add_to_cart" class="btn btn-success mt-3" disabled>Aggiungi al Carrello</button>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function() {
const variationSelect = $('#variation_select');
const classTypeSelect = $('#class_type_select');
const priceDisplay = $('#price_display');
const addToCartButton = $('#add_to_cart');
function updatePriceAndButton() {
const selectedVariation = variationSelect.val();
const selectedClassType = classTypeSelect.val();
if (selectedVariation && selectedClassType) {
const price = variationSelect.find('option:selected').data('price');
priceDisplay.text(`Prezzo: ${price}€`);
addToCartButton.prop('disabled', false);
} else {
priceDisplay.text('Prezzo: -- €');
addToCartButton.prop('disabled', true);
}
}
variationSelect.on('change', updatePriceAndButton);
classTypeSelect.on('change', updatePriceAndButton);
addToCartButton.on('click', function() {
const variationId = variationSelect.val();
const classTypeId = classTypeSelect.val();
alert(`Prodotto aggiunto al carrello!\nVariazione: ${variationSelect.find('option:selected').text()}\nClasse: ${classTypeSelect.find('option:selected').text()}`);
// Qui puoi aggiungere una chiamata AJAX per aggiungere al carrello
});
});
</script>
</body>
</html>