This commit is contained in:
commit
a3620a1f5f
1042 changed files with 226722 additions and 0 deletions
46
site/templates/validate.json.php
Normal file
46
site/templates/validate.json.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue