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-173-harmony: Harmony > Cart drawer > Format currency based on Dotshop locale #62

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Changes from 5 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
23 changes: 19 additions & 4 deletions themes/harmony/assets/add-to-cart.js
Original file line number Diff line number Diff line change
@@ -160,10 +160,12 @@ function cartTemplate(item) {
</div>
<div class="left-items">
<div class="product-price">
<span class="compare-price">${item.productVariant.compare_at_price ? `${item.productVariant.compare_at_price} ${currencyCode}` : ''}</span>
${
item.productVariant.compare_at_price ?
`<span class="compare-price">${item.productVariant.compare_at_price}</span>` : ''
}
<div class="currency-wrapper">
<span class="price">${item.productVariant.price}</span>
<span class="currency-code">${currencyCode}</span>
</div>
</div>
<button class="remove-item-btn" aria-label="remove item">
@@ -212,6 +214,18 @@ async function updateCartDrawer() {
const products = document.createElement('ul');

for (const item of cartData.items) {
item.price = formatCurrency(item.price, currencyCode, customerLocale);
item.productVariant.price = formatCurrency(item.productVariant.price, currencyCode, customerLocale);

if (item.productVariant.compare_at_price) {
const usePrecision = shouldUsePrecision(item.productVariant.compare_at_price);
item.productVariant.compare_at_price = formatCurrency(
item.productVariant.compare_at_price,
currencyCode,
customerLocale,
);
}

products.innerHTML += cartTemplate(item);
}

@@ -227,13 +241,14 @@ async function updateCartDrawer() {
cartDrawerContent.appendChild(p);
}

cartData.sub_total = formatCurrency(cartData.sub_total, currencyCode, customerLocale);

const footerContainerHTML = `
<div class="footer">
<div class="price-wrapper">
<span class="total-price">${CART_DRAWER_TRANSLATION.totalAmount}</span>
<div class="currency-wrapper">
<span class="currency-value">${cartData.sub_total.toFixed(2)}</span>
<span class="currency-code">${currencyCode}</span>
<span class="currency-value">${cartData.sub_total}</span>
</div>
<span class="spinner footer-spinner" style="display: none;"></span>
</div>
45 changes: 45 additions & 0 deletions themes/harmony/assets/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* ----- Store currency ----- */
const currencyCode = window.Dotshop.currency;
const customerLocale = window.Dotshop.customer_locale;
/* ------------------ */
/* ----- navbar ----- */
/* ------------------ */
@@ -301,3 +302,47 @@ if (FORM.errors) {
let decodedText = decodeHtmlEntities(FORM.errors);
notify(renderTextContent(decodedText), 'error', 20000);
}

/**
* Formats a given amount into a currency string.
*
* @param {number} amount - The numerical value to be formatted.
* @param {string} currencySymbol - The symbol of the currency (e.g., $, €, £, USD, EUR, etc).
* @param {string} [locale='en-US'] - Optional. The locale string to format the currency (e.g., 'en-US', 'fr-FR').
* @param {boolean} [usePrecision=false] - Optional. Whether to include decimal precision.
* @returns {string} - The formatted currency string.
*/
function formatCurrency(amount, currencySymbol, locale = 'en-US') {
const usePrecision = shouldUsePrecision(amount);
const formatter = new Intl.NumberFormat(locale, {
style: 'decimal',
maximumFractionDigits: usePrecision ? 2 : 0,
roundingMode: 'floor'
});

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');

return symbolIndex === 0
? `${currencySymbol} ${formattedValue}`
: `${formattedValue} ${currencySymbol}`;
}

function shouldUsePrecision(amount) {
const { multicurrency_settings } = Dotshop.store;
const { isMulticurrencyActive, usePrecision } = multicurrency_settings;

if (!isMulticurrencyActive) {
return !(amount % 1 === 0)
Othman-Charai marked this conversation as resolved.
Show resolved Hide resolved
}

return isMulticurrencyActive && usePrecision;
}
24 changes: 0 additions & 24 deletions themes/harmony/assets/money.js

This file was deleted.

12 changes: 2 additions & 10 deletions themes/harmony/assets/product.js
Original file line number Diff line number Diff line change
@@ -334,11 +334,7 @@ function updateProductDetails(parentSection, image, price, compareAtPrice) {
const productPrices = parentSection.querySelectorAll('.product-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);
const formattedPrice = formatCurrency(price, currencyCode, customerLocale);

if (productPrices.length === 0) {
if (showStickyCheckoutPrice) {
@@ -362,11 +358,7 @@ 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);
const formattedCompareAtPrice = formatCurrency(compareAtPrice, currencyCode, customerLocale);

variantCompareAtPrices.forEach(variantComparePrice => {
variantComparePrice.innerHTML = `<del> ${formattedCompareAtPrice} </del>`;
1 change: 0 additions & 1 deletion themes/harmony/sections/product.liquid
Original file line number Diff line number Diff line change
@@ -472,5 +472,4 @@
{% endif %}
{%- endjavascript -%}

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