46 lines
1.4 KiB
PHP
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);
|