Hello,
Thank you for all these details, I managed to display the stock.
My brother also helped me a lot to make the PHP code and I thank him;)
Here is the code that manages:
-
In stock (If you have not indicated anything in snipcart.com (Unspecified) we consider that it is in stock)
-
In stock. Only X copies left in stock. (stock indicated in snipcart.com)
-
Out of stock. This product is being restocked. + Email form. (stock at 0 in snipcart.com)
The result of the 3 possibilities in image side by side:
This code:
I. The same code as in my first message
<?php
function call_snipcart_api($url, $method = "GET", $post_data = null) {
$url = 'https://app.snipcart.com/api' . $url;
$query = curl_init();
$headers = array();
$headers[] = 'Content-type: application/json';
if ($post_data)
$headers[] = 'Content-Length: ' . strlen($post_data);
$headers[] = 'Accept: application/json';
// IMPORTANT: CREATE "SECRET API KEYS" ON SNIPCART.COM
$secret = 'API_KEY_HERE';
$headers[] = 'Authorization: Basic '.base64_encode($secret . ":");
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0
);
if ($post_data) {
$options[CURLOPT_CUSTOMREQUEST] = $method;
$options[CURLOPT_POSTFIELDS] = $post_data;
}
curl_setopt_array($query, $options);
$resp = curl_exec($query);
curl_close($query);
return json_decode($resp);
}
?>
II) PRODUCT CODE
// ON THE TOP: Name, description, price ...
<?php
// SNIPPET API CODE
snippet('shop/api');
// URL API (https://app.snipcart.com/api/**products**) + SLUG PRODUCT
$orders = call_snipcart_api('/products/'.$page->slug());
?>
// THE 3 POSSIBLE DISPLAYS
<?php if ($orders): ?>
<?php $has_stock = false; ?>
<?php if (isset($orders->stock) && $orders->stock == 0): ?>
<br /><small><span style="font-size:75%;">Victime de son succès. Ce produit est en cours de réapprovisionnement.</span></small>
// OPTIONAL
<?php snippet('shop/form/shop-product') ?>
<?php elseif (isset($orders->stock) && $orders->stock > 0): ?>
<small><sup class="badge badge-success">En stock</sup><br />
<span style="font-size:75%;">Il ne reste plus que <?php echo $orders->stock ?> exemplaire(s) en stock.</span>
</small>
<?php $has_stock = true; ?>
<?php else: ?>
<small><sup class="badge badge-success">En stock</sup></small>
<?php $has_stock = true; ?>
<?php endif; ?>
</h4>
<?php if($has_stock): ?>
<a href="<?= $page->url() ?>" class="btn btn-block btn-secondary cta snipcart-add-item" data-item-id="<?= $page->slug() ?>" data-item-price="<?= $page->price()->html() ?>" data-item-url="<?= $page->url() ?>" data-item-description="<?= $page->summary()->html() ?>" <?php if ($image = $page->image()): ?> data-item-image="<?= $page->image()->url() ?>" <?php else: ?> <?php endif ?> data-item-name="<?= $page->title()->html() ?>" data-item-has-taxes-included="true" data-item-custom1-name="Consigne <?php echo number_format((float)$page->depositsystem()->value(), 2, ',', ' '); ?> €" data-item-custom1-options="Consigne[+<?= $page->depositsystem()->value() ?>]" data-item-custom1-required="false" data-item-custom1-has-taxes-included="true">
<i class="fas fa-shopping-cart"></i> Ajouter au panier
</a>
<?php endif; ?>
<?php endif; ?>
III) OPTIONAL FORM (guide: https://getkirby.com/docs/cookbook/forms/basic-contact-form). And add: controllers, templates/emails …
<!-- Début formulaire SUCCESS -->
<?php if($success): ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert" data-delay="100" id="myAlert">
<strong><?= $success ?></strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php else: ?>
<!-- Début formulaire -->
<?php if (isset($alert['error'])): ?>
<div><?= $alert['error'] ?></div>
<?php endif ?>
<form method="POST" action="<?= $page->url() ?>/">
<div class="row mt-3">
<div class="honeypot">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="website" id="website" name="website">
</div>
<div class="col-md">
<div class="form-group">
<label for="product" class="sr-only">Produit</label>
<input type="text" id="product" name="product" value="<?= $page->title() ?>" class="sr-only">
<label for="product_url" class="sr-only">Url</label>
<input type="text" id="product_url" name="product_url" value="<?= $page->url() ?>" class="sr-only">
<label for="email" class="sr-only">Email <abbr title="required">*</abbr></label>
<small id="emailHelp" class="form-text mb-2">Pour être prévenu lorsque le produit est disponible:</small>
<input type="email" id="email" name="email" value="<?= $data['email'] ?? '' ?>" class="form-control p-3 rounded-0" placeholder="indiquez votre e-mail" required>
<?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
</div>
</div>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn pb_outline-light pb_font-13 pb_letter-spacing-2 p-3 rounded-0" value="Envoyez">
</div>
</form>
<?php endif ?>
<!-- fin formulaire -->
