Add virtual pages from Shopify and panel refresh button
This simplifies product management by eliminating manual Kirby page creation. Products are now automatically loaded as virtual pages from the Shopify API. Changes: - Add virtual pages via page.children:after hook - Create shopify.php helper with caching (60min TTL) - Add shopify-refresh panel plugin for cache management - Remove manual product content files (now virtual) - Update site.yml blueprint to show refresh button - Fix cache implementation to use get/set pattern Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
4987c4830f
commit
ade0ed1a67
21 changed files with 570 additions and 41 deletions
81
site/config/shopify.php
Normal file
81
site/config/shopify.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Récupère les produits Shopify avec cache (TTL 1h)
|
||||
*/
|
||||
function getShopifyProducts(): array
|
||||
{
|
||||
$cache = kirby()->cache('shopify');
|
||||
$products = $cache->get('products');
|
||||
|
||||
if ($products === null) {
|
||||
$products = fetchShopifyProducts();
|
||||
$cache->set('products', $products, 60); // Cache 60 minutes
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appel direct à l'API Shopify Storefront
|
||||
*/
|
||||
function fetchShopifyProducts(): array
|
||||
{
|
||||
$domain = 'nv7cqv-bu.myshopify.com';
|
||||
$token = 'dec3d35a2554384d149c72927d1cfd1b';
|
||||
$apiVersion = '2026-01';
|
||||
$endpoint = "https://{$domain}/api/{$apiVersion}/graphql.json";
|
||||
|
||||
$query = '
|
||||
query getAllProducts {
|
||||
products(first: 250, sortKey: TITLE) {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
handle
|
||||
title
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
$ch = curl_init($endpoint);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'X-Shopify-Storefront-Access-Token: ' . $token
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
||||
'query' => $query
|
||||
]));
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("Shopify API error: HTTP {$httpCode}");
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if (isset($data['errors'])) {
|
||||
error_log("Shopify API GraphQL errors: " . json_encode($data['errors']));
|
||||
return [];
|
||||
}
|
||||
|
||||
$products = [];
|
||||
foreach ($data['data']['products']['edges'] as $edge) {
|
||||
$node = $edge['node'];
|
||||
$products[] = [
|
||||
'id' => $node['id'],
|
||||
'handle' => $node['handle'],
|
||||
'title' => $node['title']
|
||||
];
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue