index-shop/site/snippets/structured-data-product.php

85 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/**
* Structured Data (JSON-LD) for Product pages
* This will be populated by JavaScript after Shopify data is loaded
*/
?>
<script type="application/ld+json" id="product-schema">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "<?= $page->title() ?>",
"description": "",
"image": [],
"offers": {
"@type": "Offer",
"url": "<?= $page->url() ?>",
"priceCurrency": "EUR",
"price": "0",
"availability": "https://schema.org/InStock",
"seller": {
"@type": "Organization",
"name": "Index.ngo"
}
},
"brand": {
"@type": "Organization",
"name": "Index.ngo"
}
}
</script>
<script>
// Update structured data when product loads
(function() {
const container = document.querySelector('[data-product-loader]');
if (!container) return;
const handle = container.dataset.shopifyHandle;
const language = container.dataset.language || 'fr';
const isEnglish = language === 'en';
const cart = new ShopifyCart({
domain: 'nv7cqv-bu.myshopify.com',
storefrontAccessToken: 'dec3d35a2554384d149c72927d1cfd1b'
});
cart.getProductByHandle(handle).then(product => {
if (!product) return;
const title = isEnglish && product.titleEn?.value ? product.titleEn.value : product.title;
const description = isEnglish && product.descriptionEn?.value ? product.descriptionEn.value : product.description;
const price = parseFloat(product.priceRange.minVariantPrice.amount);
const images = product.images.edges.map(edge => edge.node.url);
const availability = product.availableForSale ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock';
const schema = {
"@context": "https://schema.org/",
"@type": "Product",
"name": title,
"description": description,
"image": images,
"offers": {
"@type": "Offer",
"url": window.location.href,
"priceCurrency": "EUR",
"price": price.toFixed(2),
"availability": availability,
"seller": {
"@type": "Organization",
"name": "Index.ngo"
}
},
"brand": {
"@type": "Organization",
"name": "Index.ngo"
}
};
const schemaScript = document.getElementById('product-schema');
if (schemaScript) {
schemaScript.textContent = JSON.stringify(schema, null, 2);
}
});
})();
</script>