added shop example with variation
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
include('include/headscript.php');
|
||||
|
||||
|
||||
|
||||
$dbHandler = DBHandlerSelect::getInstance();
|
||||
$pdo = $dbHandler->getConnection();
|
||||
|
||||
// Verifica che iduserlogin sia definito
|
||||
if (!isset($iduserlogin)) {
|
||||
die("Errore: ID utente non definito.");
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID utente non definito']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Recupera i dati della scuola in base all'utente loggato
|
||||
@@ -20,22 +20,80 @@ $stmt = $pdo->prepare("
|
||||
$stmt->execute([$iduserlogin]);
|
||||
$school = $stmt->fetch();
|
||||
if (!$school) {
|
||||
die("Errore: Nessuna scuola trovata per l'utente loggato.");
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Nessuna scuola trovata per l\'utente loggato']);
|
||||
exit;
|
||||
}
|
||||
$school_id = $school['id'];
|
||||
|
||||
$product_id = $_GET['product_id'] ?? 0;
|
||||
$variation_id = $_GET['variation_id'] ?? 0;
|
||||
|
||||
// Verifica che il prodotto appartenga alla scuola
|
||||
$stmt = $pdo->prepare("SELECT id FROM products WHERE id = ? AND school_id = ?");
|
||||
if ($product_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID prodotto non valido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Recupera i dettagli del prodotto (inclusi is_full_access e auto_propagate_to_order)
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT id, is_full_access, auto_propagate_to_order
|
||||
FROM products
|
||||
WHERE id = ? AND school_id = ?
|
||||
");
|
||||
$stmt->execute([$product_id, $school_id]);
|
||||
if (!$stmt->fetch()) {
|
||||
die("Errore: Prodotto non trovato o non autorizzato.");
|
||||
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$product) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Prodotto non trovato o non autorizzato']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Inizializza l'array di risposta con i dettagli del prodotto
|
||||
$response = [
|
||||
'is_full_access' => $product['is_full_access'],
|
||||
'auto_propagate_to_order' => $product['auto_propagate_to_order'],
|
||||
'class_types' => []
|
||||
];
|
||||
|
||||
// Se variation_id è specificato, recupera i dettagli della variazione
|
||||
if ($variation_id > 0) {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT id, auto_propagate_to_order
|
||||
FROM product_variations
|
||||
WHERE id = ? AND product_id = ?
|
||||
");
|
||||
$stmt->execute([$variation_id, $product_id]);
|
||||
$variation = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$variation) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Variazione non trovata o non autorizzata']);
|
||||
exit;
|
||||
}
|
||||
// Sovrascrivi auto_propagate_to_order con il valore della variazione
|
||||
$response['auto_propagate_to_order'] = $variation['auto_propagate_to_order'];
|
||||
}
|
||||
|
||||
// Recupera le classi associate
|
||||
if ($variation_id > 0) {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT class_type_id
|
||||
FROM product_class_types
|
||||
WHERE product_id = ? AND variation_id = ?
|
||||
");
|
||||
$stmt->execute([$product_id, $variation_id]);
|
||||
} else {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT class_type_id
|
||||
FROM product_class_types
|
||||
WHERE product_id = ? AND variation_id IS NULL
|
||||
");
|
||||
$stmt->execute([$product_id]);
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("SELECT class_type_id FROM product_class_types WHERE product_id = ?");
|
||||
$stmt->execute([$product_id]);
|
||||
$class_types = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
$response['class_types'] = $class_types;
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($class_types);
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user