- Add product loaders (product-loader.js, products-list-loader.js) to fetch data from Shopify - Extend Shopify API client with getProductByHandle() and getAllProducts() methods - Integrate Shopify metafields for multilingual support (custom.title_en, custom.description_en) - Refactor product.php and home.php templates to load content dynamically - Simplify product blueprint to minimal routing configuration - Create generic buy-button.php snippet with variant selection - Update footer.php with conditional script loading - Refactor _section--product.scss for better Sass structure - Add translations for loading states and product errors - Clean up old Kirby product content files Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
(function() {
|
|
const cart = new ShopifyCart({
|
|
domain: 'nv7cqv-bu.myshopify.com',
|
|
storefrontAccessToken: 'dec3d35a2554384d149c72927d1cfd1b'
|
|
});
|
|
|
|
const addToCartBtn = document.querySelector('[data-shopify-add-to-cart]');
|
|
|
|
if (!addToCartBtn) {
|
|
return;
|
|
}
|
|
|
|
const texts = {
|
|
add: addToCartBtn.dataset.textAdd || 'Add to cart',
|
|
adding: addToCartBtn.dataset.textAdding || 'Adding...',
|
|
added: addToCartBtn.dataset.textAdded || 'Added! ✓',
|
|
error: addToCartBtn.dataset.textError || 'Error - Try again'
|
|
};
|
|
|
|
addToCartBtn.addEventListener('click', async function(e) {
|
|
e.preventDefault();
|
|
|
|
const variantId = this.dataset.variantId;
|
|
|
|
if (!variantId) {
|
|
console.error('No variant ID found');
|
|
return;
|
|
}
|
|
|
|
addToCartBtn.disabled = true;
|
|
const originalText = addToCartBtn.textContent;
|
|
addToCartBtn.textContent = texts.adding;
|
|
|
|
try {
|
|
const cartResult = await cart.addToCart(variantId, 1);
|
|
|
|
addToCartBtn.textContent = texts.added;
|
|
addToCartBtn.classList.add('success');
|
|
|
|
document.dispatchEvent(new CustomEvent('cart:updated', {
|
|
detail: { cart: cartResult }
|
|
}));
|
|
|
|
setTimeout(() => {
|
|
addToCartBtn.disabled = false;
|
|
addToCartBtn.textContent = originalText;
|
|
addToCartBtn.classList.remove('success');
|
|
}, 1500);
|
|
|
|
} catch (error) {
|
|
console.error('Error adding to cart:', error);
|
|
|
|
addToCartBtn.textContent = texts.error;
|
|
addToCartBtn.classList.add('error');
|
|
|
|
setTimeout(() => {
|
|
addToCartBtn.disabled = false;
|
|
addToCartBtn.textContent = originalText;
|
|
addToCartBtn.classList.remove('error');
|
|
}, 2000);
|
|
}
|
|
});
|
|
})();
|