Fix cart initialization and ShopifyCart loading

- Wait for ShopifyCart to be available before initializing structured data
- Add getCart() method to retrieve existing cart from Shopify API
- Load cart state on page load to display correct initial cart contents

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
isUnknown 2026-01-21 11:06:28 +01:00
parent 9396ae4e02
commit aa873e117f
3 changed files with 83 additions and 6 deletions

View file

@ -38,12 +38,18 @@
const language = container.dataset.language || 'fr';
const isEnglish = language === 'en';
const cart = new ShopifyCart({
domain: 'nv7cqv-bu.myshopify.com',
storefrontAccessToken: 'dec3d35a2554384d149c72927d1cfd1b'
});
function initStructuredData() {
if (typeof ShopifyCart === 'undefined') {
setTimeout(initStructuredData, 100);
return;
}
cart.getProductByHandle(handle).then(product => {
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;
@ -79,6 +85,10 @@
if (schemaScript) {
schemaScript.textContent = JSON.stringify(schema, null, 2);
}
});
});
}
// Initialize when ShopifyCart is available
initStructuredData();
})();
</script>