Blog 

Sidofält

RECENT ARTICLES

Jet ädelstenar

Den Av riyo de / 0 kommentarer
c) : []; this.tag = this.dataset.tag || ''; this.limit = parseInt(this.dataset.limit) || 4; this.enableRandom = this.dataset.enableRandom === 'true'; this.titleWrap = this.dataset.titleWrap || 'truncate'; this.titleLines = parseInt(this.dataset.titleLines) || 2; } connectedCallback() { if (this.enableRandom && this.collections.length > 0) { this.fetchRandomProducts(); } } async fetchRandomProducts() { const productGrid = this.querySelector('[data-product-grid]'); if (!productGrid) return; try { const allProducts = []; for (const collectionHandle of this.collections) { const response = await fetch(`/collections/${collectionHandle}/products.json?limit=250`); const data = await response.json(); let products = data.products; if (this.tag) { products = products.filter(product => product.tags.some(tag => tag.toLowerCase() === this.tag.toLowerCase()) ); } allProducts.push(...products); } if (allProducts.length === 0) { productGrid.innerHTML = `

No products found in the selected collections

`; return; } const shuffled = allProducts.sort(() => 0.5 - Math.random()); const selectedProducts = shuffled.slice(0, this.limit); productGrid.innerHTML = selectedProducts.map(product => this.renderProductCard(product)).join(''); } catch (error) { console.error('Error fetching products:', error); productGrid.innerHTML = `

Error loading products. Please try again.

`; } } getTitleStyles() { if (this.titleWrap === 'truncate') { return ` display: -webkit-box; -webkit-line-clamp: ${this.titleLines}; -webkit-box-orient: vertical; overflow: hidden; `; } else if (this.titleWrap === 'nowrap') { return ` white-space: nowrap; overflow: hidden; text-overflow: ellipsis; `; } else { return 'word-wrap: break-word;'; } } renderProductCard(product) { const showVendor = false; const showPrice = true; const showButton = true; const showSaleBadge = false; const buttonText = "View product"; const price = product.variants[0].price; const comparePrice = product.variants[0].compare_at_price || null; const onSale = comparePrice && comparePrice > price; const formatMoney = (cents) => { if (typeof Shopify !== 'undefined' && Shopify.formatMoney) { return Shopify.formatMoney(cents, '${{amount}}'); } return '$' + (cents / 100).toFixed(2); }; let imageUrl = null; if (product.featured_image) { imageUrl = product.featured_image; } else if (product.image && product.image.src) { imageUrl = product.image.src; } else if (product.images && product.images.length > 0) { imageUrl = product.images[0].src || product.images[0]; } const titleStyles = this.getTitleStyles(); return `
${imageUrl ? `${this.escapeHtml(product.title)}` : `
` }
${showVendor && product.vendor ? `
${this.escapeHtml(product.vendor)}
` : ''}

${this.escapeHtml(product.title)}

${showPrice ? `
${formatMoney(price)} ${onSale ? ` ${formatMoney(comparePrice)} ${showSaleBadge ? 'Sale' : ''} ` : ''}
` : ''} ${showButton ? ` ${this.escapeHtml(buttonText)} ` : ''}
`; } escapeHtml(text) { const map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return text.replace(/[&<>"']/g, m => map[m]); } } customElements.define('random-product-cards-aykllv3kxtev1c3p4raigenblock78d8a85ipipyx', RandomProductCardsaykllv3kxtev1c3p4raigenblock78d8a85ipipyx); })();

Loading random products...