index-shop/site/templates/validate.json.php
isUnknown a3620a1f5f
Some checks are pending
Deploy / Deploy to Production (push) Waiting to run
first commit
2025-12-10 15:12:06 +01:00

46 lines
1.4 KiB
PHP

<?php
// Template pour valider les produits avec Snipcart
// Ce fichier sera appelé par Snipcart pour valider le stock avant l'ajout au panier
header('Content-Type: application/json');
$product = $page;
// Vérifier que c'est bien une page produit
if ($product->intendedTemplate() !== 'product') {
http_response_code(404);
echo json_encode(['error' => 'Product not found']);
exit;
}
// Récupérer le stock actuel
$stock = (int) $product->stock()->value();
// Préparer la réponse JSON pour Snipcart
$response = [
'id' => $product->slug(),
'price' => (float) $product->price()->value(),
'url' => $product->url() . '/validate.json',
'name' => $product->title()->value(),
'description' => $product->description()->value(),
'image' => $product->images()->first() ? $product->images()->first()->url() : '',
'inventory' => $stock,
'stock' => $stock
];
// Ajouter les options si disponibles
if ($product->hasOptions()->toBool() && $product->optionValues()->isNotEmpty()) {
$values = $product->optionValues()->split(',');
$trimmedValues = array_map('trim', $values);
$snipcartOptions = implode('|', $trimmedValues);
$response['customFields'] = [
[
'name' => $product->optionLabel()->value(),
'options' => $snipcartOptions,
'required' => true
]
];
}
echo json_encode($response);