From cc3e9febba2f9aa18147d4f6f8e649834e217405 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:56:32 +0100 Subject: [PATCH 01/13] add formatter method --- themes/aura/assets/product.js | 59 ++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index b1302bc0..4362a0b2 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -316,6 +316,42 @@ function getSelectedVariant(parentSection) { }); } +function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = false) { + const shouldUsePercision = !(amount % 1 === 0) || usePercision; + + try { + const formatter = new Intl.NumberFormat(locale, { + style: 'currency', + currency: currencySymbol, + minimumFractionDigits: shouldUsePercision ? 2 : 0, + }); + + return formatter.format(amount); + } catch (error) { + // Fallback formatting + const formatter = new Intl.NumberFormat(locale, { + style: 'decimal', + minimumFractionDigits: shouldUsePercision ? 2 : 0, + }); + + const formattedValue = formatter.format(amount); + + const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { + style: 'currency', + currency: 'USD', + currencyDisplay: 'symbol', + }); + + const parts = determineSymbolPositionFormatter.formatToParts(1); + const symbolIndex = parts.findIndex(part => part.type === 'currency'); + const isSymbolOnRight = symbolIndex === 0; + + return isSymbolOnRight + ? `${currencySymbol} ${formattedValue}` + : `${formattedValue} ${currencySymbol}`; + } +} + /** * Updates product details after variant change * @param {HTMLElement} parentSection @@ -326,26 +362,25 @@ function getSelectedVariant(parentSection) { function updateProductDetails(parentSection, image, price, compareAtPrice) { if (image) { const mainImgs = parentSection.querySelectorAll('.main-image'); - - mainImgs.forEach(mainImg => mainImg.src = image) + mainImgs.forEach(mainImg => mainImg.src = image); } if (price) { const productPrices = parentSection.querySelectorAll('.product-price'); - const showStickyCheckoutPrice = $('#sticky-price'); + const showStickyCheckoutPrice = document.getElementById('sticky-price'); + const usePercision = Dotshop?.store?.multicurrency_settings.usePrecision; + + const formattedPrice = formatCurrency(price, Dotshop.currency, Dotshop.customer_locale, usePercision); if (productPrices.length === 0) { if (showStickyCheckoutPrice) { - showStickyCheckoutPrice.innerHTML = `${price} ${Dotshop.currency}`; + showStickyCheckoutPrice.innerHTML = formattedPrice; } - return; } productPrices.forEach(productPrice => { - const displayValue = `${price} ${Dotshop.currency}`; - - productPrice.innerText = displayValue; + productPrice.innerText = formattedPrice; if (showStickyCheckoutPrice) { showStickyCheckoutPrice.innerHTML = productPrice.innerHTML; @@ -356,13 +391,15 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price'); if (compareAtPrice) { + const usePercision = Dotshop?.store?.multicurrency_settings.usePrecision; + const formattedCompareAtPrice = formatCurrency(compareAtPrice, Dotshop.currency, Dotshop.customer_locale, usePercision); variantCompareAtPrices.forEach(variantComparePrice => { - variantComparePrice.innerHTML = ` ${compareAtPrice} ${Dotshop.currency} `; - }) + variantComparePrice.innerHTML = `${formattedCompareAtPrice}`; + }); } else { variantCompareAtPrices.forEach(variantComparePrice => { variantComparePrice.innerHTML = ``; - }) + }); } goToCheckoutStep(); From 8c02f6f0534a6c1eaf9d924d136b13744fa23879 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:29:53 +0100 Subject: [PATCH 02/13] extract format function to external script --- themes/aura/assets/money.js | 35 ++++++++++++++++++++++++++++ themes/aura/assets/product.js | 36 ----------------------------- themes/aura/sections/product.liquid | 1 + 3 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 themes/aura/assets/money.js diff --git a/themes/aura/assets/money.js b/themes/aura/assets/money.js new file mode 100644 index 00000000..0b508f8a --- /dev/null +++ b/themes/aura/assets/money.js @@ -0,0 +1,35 @@ +function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = false) { + const shouldUsePercision = !(amount % 1 === 0) || usePercision; + + try { + const formatter = new Intl.NumberFormat(locale, { + style: 'currency', + currency: currencySymbol, + minimumFractionDigits: shouldUsePercision ? 2 : 0, + }); + + return formatter.format(amount); + } catch (error) { + // Fallback formatting for when the currency symbol is invalid + const formatter = new Intl.NumberFormat(locale, { + style: 'decimal', + minimumFractionDigits: shouldUsePercision ? 2 : 0, + }); + + const formattedValue = formatter.format(amount); + + const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { + style: 'currency', + currency: 'USD', + currencyDisplay: 'symbol', + }); + + const parts = determineSymbolPositionFormatter.formatToParts(1); + const symbolIndex = parts.findIndex(part => part.type === 'currency'); + const isSymbolOnRight = symbolIndex === 0; + + return isSymbolOnRight + ? `${currencySymbol} ${formattedValue}` + : `${formattedValue} ${currencySymbol}`; + } +} \ No newline at end of file diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index 4362a0b2..f0a9e6ea 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -316,42 +316,6 @@ function getSelectedVariant(parentSection) { }); } -function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = false) { - const shouldUsePercision = !(amount % 1 === 0) || usePercision; - - try { - const formatter = new Intl.NumberFormat(locale, { - style: 'currency', - currency: currencySymbol, - minimumFractionDigits: shouldUsePercision ? 2 : 0, - }); - - return formatter.format(amount); - } catch (error) { - // Fallback formatting - const formatter = new Intl.NumberFormat(locale, { - style: 'decimal', - minimumFractionDigits: shouldUsePercision ? 2 : 0, - }); - - const formattedValue = formatter.format(amount); - - const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { - style: 'currency', - currency: 'USD', - currencyDisplay: 'symbol', - }); - - const parts = determineSymbolPositionFormatter.formatToParts(1); - const symbolIndex = parts.findIndex(part => part.type === 'currency'); - const isSymbolOnRight = symbolIndex === 0; - - return isSymbolOnRight - ? `${currencySymbol} ${formattedValue}` - : `${formattedValue} ${currencySymbol}`; - } -} - /** * Updates product details after variant change * @param {HTMLElement} parentSection diff --git a/themes/aura/sections/product.liquid b/themes/aura/sections/product.liquid index 5497ad43..ff7a885a 100644 --- a/themes/aura/sections/product.liquid +++ b/themes/aura/sections/product.liquid @@ -473,4 +473,5 @@ {% endif %} {%- endjavascript -%} +{{ 'money.js' | asset_url | script_tag }} {{ 'product.js' | asset_url | script_tag_deferred }} From b6f20bc72776b4e61a5b90766d7f50e60a4b57fa Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 08:15:24 +0100 Subject: [PATCH 03/13] fix wrong var name --- themes/aura/assets/money.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/aura/assets/money.js b/themes/aura/assets/money.js index 0b508f8a..53a593c4 100644 --- a/themes/aura/assets/money.js +++ b/themes/aura/assets/money.js @@ -26,9 +26,9 @@ function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = const parts = determineSymbolPositionFormatter.formatToParts(1); const symbolIndex = parts.findIndex(part => part.type === 'currency'); - const isSymbolOnRight = symbolIndex === 0; + const isSymbolOnLeft = symbolIndex === 0; - return isSymbolOnRight + return isSymbolOnLeft ? `${currencySymbol} ${formattedValue}` : `${formattedValue} ${currencySymbol}`; } From 4b62e3ed2a3954cf6b3f19f07c4f2738f8530da4 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:17:25 +0100 Subject: [PATCH 04/13] add EOF line to money.js --- themes/aura/assets/money.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/aura/assets/money.js b/themes/aura/assets/money.js index 53a593c4..161b9917 100644 --- a/themes/aura/assets/money.js +++ b/themes/aura/assets/money.js @@ -32,4 +32,4 @@ function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = ? `${currencySymbol} ${formattedValue}` : `${formattedValue} ${currencySymbol}`; } -} \ No newline at end of file +} From a9c8406a17f726564bfab0a3d86f4455abed22b1 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:18:01 +0100 Subject: [PATCH 05/13] fix percision issue --- themes/aura/assets/product.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index f0a9e6ea..d75a3782 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -332,7 +332,8 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (price) { const productPrices = parentSection.querySelectorAll('.product-price'); const showStickyCheckoutPrice = document.getElementById('sticky-price'); - const usePercision = Dotshop?.store?.multicurrency_settings.usePrecision; + const isMultiCurrencyActive = otshop?.store?.multicurrency_settings.isMulticurrencyActive; + const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings.usePrecision; const formattedPrice = formatCurrency(price, Dotshop.currency, Dotshop.customer_locale, usePercision); @@ -355,7 +356,8 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price'); if (compareAtPrice) { - const usePercision = Dotshop?.store?.multicurrency_settings.usePrecision; + const isMultiCurrencyActive = otshop?.store?.multicurrency_settings.isMulticurrencyActive; + const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings.usePrecision; const formattedCompareAtPrice = formatCurrency(compareAtPrice, Dotshop.currency, Dotshop.customer_locale, usePercision); variantCompareAtPrices.forEach(variantComparePrice => { variantComparePrice.innerHTML = `${formattedCompareAtPrice}`; From d30763d5e594df79c2d6f577944c0d48cf56b0e5 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:40:55 +0100 Subject: [PATCH 06/13] optional chaining --- themes/aura/assets/product.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index d75a3782..9bf26148 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -332,8 +332,8 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (price) { const productPrices = parentSection.querySelectorAll('.product-price'); const showStickyCheckoutPrice = document.getElementById('sticky-price'); - const isMultiCurrencyActive = otshop?.store?.multicurrency_settings.isMulticurrencyActive; - const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings.usePrecision; + const isMultiCurrencyActive = otshop?.store?.multicurrency_settings?.isMulticurrencyActive; + const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; const formattedPrice = formatCurrency(price, Dotshop.currency, Dotshop.customer_locale, usePercision); @@ -356,8 +356,8 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price'); if (compareAtPrice) { - const isMultiCurrencyActive = otshop?.store?.multicurrency_settings.isMulticurrencyActive; - const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings.usePrecision; + const isMultiCurrencyActive = otshop?.store?.multicurrency_settings?.isMulticurrencyActive; + const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; const formattedCompareAtPrice = formatCurrency(compareAtPrice, Dotshop.currency, Dotshop.customer_locale, usePercision); variantCompareAtPrices.forEach(variantComparePrice => { variantComparePrice.innerHTML = `${formattedCompareAtPrice}`; From d9e134f19d392826595aa6ed7e518b85d5bf6b43 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:41:28 +0100 Subject: [PATCH 07/13] big oopsie --- themes/aura/assets/product.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index 9bf26148..30d7705a 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -332,7 +332,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (price) { const productPrices = parentSection.querySelectorAll('.product-price'); const showStickyCheckoutPrice = document.getElementById('sticky-price'); - const isMultiCurrencyActive = otshop?.store?.multicurrency_settings?.isMulticurrencyActive; + const isMultiCurrencyActive = Dotshop?.store?.multicurrency_settings?.isMulticurrencyActive; const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; const formattedPrice = formatCurrency(price, Dotshop.currency, Dotshop.customer_locale, usePercision); @@ -356,7 +356,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price'); if (compareAtPrice) { - const isMultiCurrencyActive = otshop?.store?.multicurrency_settings?.isMulticurrencyActive; + const isMultiCurrencyActive = Dotshop?.store?.multicurrency_settings?.isMulticurrencyActive; const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; const formattedCompareAtPrice = formatCurrency(compareAtPrice, Dotshop.currency, Dotshop.customer_locale, usePercision); variantCompareAtPrices.forEach(variantComparePrice => { From 8965b15efc53d1c80edae00174826b7407f3c078 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:57:16 +0100 Subject: [PATCH 08/13] tiny refactor --- themes/aura/assets/product.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index 30d7705a..d454783d 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -332,10 +332,12 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (price) { const productPrices = parentSection.querySelectorAll('.product-price'); const showStickyCheckoutPrice = document.getElementById('sticky-price'); - const isMultiCurrencyActive = Dotshop?.store?.multicurrency_settings?.isMulticurrencyActive; - const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; + + const { store, currency, customer_locale } = Dotshop; + const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings + const shouldUsePercision = isMulticurrencyActive && usePrecision; - const formattedPrice = formatCurrency(price, Dotshop.currency, Dotshop.customer_locale, usePercision); + const formattedPrice = formatCurrency(price, currency, customer_locale, shouldUsePercision); if (productPrices.length === 0) { if (showStickyCheckoutPrice) { @@ -356,9 +358,12 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price'); if (compareAtPrice) { - const isMultiCurrencyActive = Dotshop?.store?.multicurrency_settings?.isMulticurrencyActive; - const usePercision = isMultiCurrencyActive && Dotshop?.store?.multicurrency_settings?.usePrecision; - const formattedCompareAtPrice = formatCurrency(compareAtPrice, Dotshop.currency, Dotshop.customer_locale, usePercision); + const { store, currency, customer_locale } = Dotshop; + const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings + const shouldUsePercision = isMulticurrencyActive && usePrecision; + + const formattedCompareAtPrice = formatCurrency(compareAtPrice, currency, customer_locale, shouldUsePercision); + variantCompareAtPrices.forEach(variantComparePrice => { variantComparePrice.innerHTML = `${formattedCompareAtPrice}`; }); From 352dfe484c2f1e2409aea06dbf0b1d89ba928c8f Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:58:49 +0100 Subject: [PATCH 09/13] undo line removal --- themes/aura/assets/product.js | 1 + 1 file changed, 1 insertion(+) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index d454783d..cc5b0e29 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -343,6 +343,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (showStickyCheckoutPrice) { showStickyCheckoutPrice.innerHTML = formattedPrice; } + return; } From 3bf1836e8185c3a7507f542a787c682bddbdd6e4 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:19:52 +0100 Subject: [PATCH 10/13] Update themes/aura/assets/product.js Co-authored-by: Anas <93322743+bj-anas@users.noreply.github.com> --- themes/aura/assets/product.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index cc5b0e29..4861ab66 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -334,7 +334,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const showStickyCheckoutPrice = document.getElementById('sticky-price'); const { store, currency, customer_locale } = Dotshop; - const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings + const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings; const shouldUsePercision = isMulticurrencyActive && usePrecision; const formattedPrice = formatCurrency(price, currency, customer_locale, shouldUsePercision); From 5d7999c110f34b1acf43238de83365f2fb4a5d12 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:23:49 +0100 Subject: [PATCH 11/13] cleanup --- themes/aura/assets/product.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index 4861ab66..971e3109 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -360,7 +360,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (compareAtPrice) { const { store, currency, customer_locale } = Dotshop; - const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings + const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings; const shouldUsePercision = isMulticurrencyActive && usePrecision; const formattedCompareAtPrice = formatCurrency(compareAtPrice, currency, customer_locale, shouldUsePercision); From edd9a59f585e0ccb7685fd115d0bacbac4816ae5 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:35:32 +0100 Subject: [PATCH 12/13] fix typo --- themes/aura/assets/money.js | 8 ++++---- themes/aura/assets/product.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/themes/aura/assets/money.js b/themes/aura/assets/money.js index 161b9917..d53f61ae 100644 --- a/themes/aura/assets/money.js +++ b/themes/aura/assets/money.js @@ -1,11 +1,11 @@ -function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = false) { - const shouldUsePercision = !(amount % 1 === 0) || usePercision; +function formatCurrency(amount, currencySymbol, locale = 'en-US', usePrecision = false) { + const shouldUsePrecision = !(amount % 1 === 0) || usePrecision; try { const formatter = new Intl.NumberFormat(locale, { style: 'currency', currency: currencySymbol, - minimumFractionDigits: shouldUsePercision ? 2 : 0, + minimumFractionDigits: shouldUsePrecision ? 2 : 0, }); return formatter.format(amount); @@ -13,7 +13,7 @@ function formatCurrency(amount, currencySymbol, locale = 'en-US', usePercision = // Fallback formatting for when the currency symbol is invalid const formatter = new Intl.NumberFormat(locale, { style: 'decimal', - minimumFractionDigits: shouldUsePercision ? 2 : 0, + minimumFractionDigits: shouldUsePrecision ? 2 : 0, }); const formattedValue = formatter.format(amount); diff --git a/themes/aura/assets/product.js b/themes/aura/assets/product.js index 971e3109..6159a500 100644 --- a/themes/aura/assets/product.js +++ b/themes/aura/assets/product.js @@ -335,9 +335,9 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { const { store, currency, customer_locale } = Dotshop; const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings; - const shouldUsePercision = isMulticurrencyActive && usePrecision; + const shouldUsePrecision = isMulticurrencyActive && usePrecision; - const formattedPrice = formatCurrency(price, currency, customer_locale, shouldUsePercision); + const formattedPrice = formatCurrency(price, currency, customer_locale, shouldUsePrecision); if (productPrices.length === 0) { if (showStickyCheckoutPrice) { @@ -361,9 +361,9 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) { if (compareAtPrice) { const { store, currency, customer_locale } = Dotshop; const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings; - const shouldUsePercision = isMulticurrencyActive && usePrecision; + const shouldUsePrecision = isMulticurrencyActive && usePrecision; - const formattedCompareAtPrice = formatCurrency(compareAtPrice, currency, customer_locale, shouldUsePercision); + const formattedCompareAtPrice = formatCurrency(compareAtPrice, currency, customer_locale, shouldUsePrecision); variantCompareAtPrices.forEach(variantComparePrice => { variantComparePrice.innerHTML = `${formattedCompareAtPrice}`; From 8aeeb6ec00ba8adb26a7d91579567aa49cb6d452 Mon Sep 17 00:00:00 2001 From: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:25:16 +0100 Subject: [PATCH 13/13] reduce money code --- themes/aura/assets/money.js | 43 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/themes/aura/assets/money.js b/themes/aura/assets/money.js index d53f61ae..bce0d4a6 100644 --- a/themes/aura/assets/money.js +++ b/themes/aura/assets/money.js @@ -1,35 +1,24 @@ function formatCurrency(amount, currencySymbol, locale = 'en-US', usePrecision = false) { const shouldUsePrecision = !(amount % 1 === 0) || usePrecision; - try { - const formatter = new Intl.NumberFormat(locale, { - style: 'currency', - currency: currencySymbol, - minimumFractionDigits: shouldUsePrecision ? 2 : 0, - }); - - return formatter.format(amount); - } catch (error) { - // Fallback formatting for when the currency symbol is invalid - const formatter = new Intl.NumberFormat(locale, { - style: 'decimal', - minimumFractionDigits: shouldUsePrecision ? 2 : 0, - }); + const formatter = new Intl.NumberFormat(locale, { + style: 'decimal', + minimumFractionDigits: shouldUsePrecision ? 2 : 0, + }); - const formattedValue = formatter.format(amount); + const formattedValue = formatter.format(amount); - const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { - style: 'currency', - currency: 'USD', - currencyDisplay: 'symbol', - }); + const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { + style: 'currency', + currency: 'USD', + currencyDisplay: 'symbol', + }); - const parts = determineSymbolPositionFormatter.formatToParts(1); - const symbolIndex = parts.findIndex(part => part.type === 'currency'); - const isSymbolOnLeft = symbolIndex === 0; + const parts = determineSymbolPositionFormatter.formatToParts(1); // format with 1 USD just to determine the position of the currency symbol + const symbolIndex = parts.findIndex(part => part.type === 'currency'); + const isSymbolOnLeft = symbolIndex === 0; - return isSymbolOnLeft - ? `${currencySymbol} ${formattedValue}` - : `${formattedValue} ${currencySymbol}`; - } + return isSymbolOnLeft + ? `${currencySymbol} ${formattedValue}` + : `${formattedValue} ${currencySymbol}`; }