Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TH-155-meraki: Format product price in product page based on a unified locale code #57

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions themes/meraki/assets/money.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function formatCurrency(amount, currencySymbol, locale = 'en-US', usePrecision = false) {
const shouldUsePrecision = !(amount % 1 === 0) || usePrecision;

const formatter = new Intl.NumberFormat(locale, {
style: 'decimal',
minimumFractionDigits: shouldUsePrecision ? 2 : 0,
});

const formattedValue = formatter.format(amount);

const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol',
});

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}`;
}
24 changes: 18 additions & 6 deletions themes/meraki/assets/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,24 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) {

if (price) {
const productPrices = parentSection.querySelectorAll('.product-price');
const showStickyCheckoutPrice = $('#sticky-price');
const showStickyCheckoutPrice = document.getElementById('sticky-price');

const { store, currency, customer_locale } = Dotshop;
const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings;
const shouldUsePrecision = isMulticurrencyActive && usePrecision;

const formattedPrice = formatCurrency(price, currency, customer_locale, shouldUsePrecision);

if (productPrices.length === 0) {
if (showStickyCheckoutPrice) {
showStickyCheckoutPrice.innerHTML = `${price} ${Dotshop.currency}`;
showStickyCheckoutPrice.innerHTML = formattedPrice;
}

return;
}

productPrices.forEach(productPrice => {
const displayValue = `${price} ${Dotshop.currency}`;
const displayValue = formattedPrice;

productPrice.innerText = displayValue;

Expand All @@ -357,13 +363,19 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) {
const variantCompareAtPrices = parentSection.querySelectorAll('.compare-price');

if (compareAtPrice) {
const { store, currency, customer_locale } = Dotshop;
const { isMulticurrencyActive, usePrecision} = store.multicurrency_settings;
const shouldUsePrecision = isMulticurrencyActive && usePrecision;

const formattedCompareAtPrice = formatCurrency(compareAtPrice, currency, customer_locale, shouldUsePrecision);

variantCompareAtPrices.forEach(variantComparePrice => {
variantComparePrice.innerHTML = `<del> ${compareAtPrice} ${Dotshop.currency} </del>`;
})
variantComparePrice.innerHTML = `<del> ${formattedCompareAtPrice} </del>`;
});
} else {
variantCompareAtPrices.forEach(variantComparePrice => {
variantComparePrice.innerHTML = ``;
})
});
}

goToCheckoutStep();
Expand Down
1 change: 1 addition & 0 deletions themes/meraki/sections/product.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,5 @@
{% endif %}
{%- endjavascript -%}

{{ 'money.js' | asset_url | script_tag }}
{{ 'product.js' | asset_url | script_tag_deferred }}