function getMoneyFormat(){ // 1) Prefer Shopify global if present if (window.Shopify && window.Shopify.money_format) return window.Shopify.money_format; // 2) Fallback: your theme JSON blob in the drawer const tpl = document.querySelector('.js-cart-items-json'); if (tpl) { try { const data = JSON.parse(tpl.textContent || tpl.innerText || '{}'); if (data.moneyFormat) return data.moneyFormat; } catch(e){} } return '$'; } function formatMoney(cents, format){ // Best: let Shopify handle it (this avoids ALL the weird token issues) if (window.Shopify && typeof window.Shopify.formatMoney === 'function') { return window.Shopify.formatMoney(cents, format || getMoneyFormat()); } // Otherwise: correct Shopify-style formatter (matches common theme behavior) format = format || getMoneyFormat(); if (typeof cents === 'string') cents = cents.replace('.', ''); cents = Number(cents || 0); const formatWithDelimiters = (number, precision = 2, thousands = ',', decimal = '.') => { if (isNaN(number) || number == null) return '0'; const fixed = (number / 100).toFixed(precision); const parts = fixed.split('.'); const dollars = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousands); return parts[1] ? dollars + decimal + parts[1] : dollars; }; const match = format.match(/\{\{\s*(\w+)\s*\}\}/); const token = match ? match[1] : 'amount'; let value = ''; switch (token) { case 'amount': value = formatWithDelimiters(cents, 2, ',', '.'); break; case 'amount_no_decimals': value = formatWithDelimiters(cents, 0, ',', '.'); break; case 'amount_with_comma_separator': value = formatWithDelimiters(cents, 2, '.', ','); break; case 'amount_no_decimals_with_comma_separator': value = formatWithDelimiters(cents, 0, '.', ','); break; default: value = formatWithDelimiters(cents, 2, ',', '.'); } return format.replace(/\{\{\s*\w+\s*\}\}/, value); }