Stiletto price formatting

Stiletto price formatting

Jonathankarlsso
Visitor
2 0 0

Hello,

 

i have an problem with the currency formatting with the stiletto theme, the problem is that all products that cost more than 1000 kr is formated as 1,000 kr and google interpreters this as 1 kr. I have tried to change the formatting under settings->general then currency formatting to {{ amount_no_decimals_with_space_separator }} but then the price dissapears when I switch between variants. Please help me.

Reply 1 (1)

Jonathankarlsso
Visitor
2 0 0

Okay, so I've solved part of the problem. In Theme.JS, I've reformatted the code so that I have the following code in the JavaScript. This ensures that the price formatting is correct for the product. Now the price also appears on the other variants, but with a thousand separator, as it updates the file without the formatting. Please help me. 

 

 

const moneyFormat = '${{amount_no_decimals}}';

function formatMoney$1(cents, format) {
if (typeof cents === 'string') {
cents = cents.replace(/\s/g, '');
}
let value = '';
const placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
const formatString = format || moneyFormat;

function formatAmountNoDecimals(cents) {
let amount = Math.floor(cents / 100);
return amount.toLocaleString('sv-SE'); // Use Swedish locale to add the space as a thousand separator
}

function formatWithDelimiters(
number,
precision = 2,
thousands = ' ',
decimal = '.'
) {
if (isNaN(number) || number == null) {
return '0';
}

number = (number / 100.0).toFixed(precision);
const parts = number.split('.');
const dollarsAmount = parts[0].replace(
/(\d)(?=(\d{3})+(?!\d))/g,
`$1${thousands}`
);
const centsAmount = parts[1] ? decimal + parts[1] : '';

return dollarsAmount + centsAmount;
}

switch (formatString.match(placeholderRegex)[1]) {
case 'amount':
value = formatWithDelimiters(cents, 2);
break;
case 'amount_no_decimals':
value = formatAmountNoDecimals(cents); // Apply the Swedish locale formatting
break;
case 'amount_with_comma_separator':
value = formatWithDelimiters(cents, 2, ' ', ',');
break;
case 'amount_no_decimals_with_comma_separator':
value = formatWithDelimiters(cents, 0, ' ', ',');
break;
}

return formatString.replace(placeholderRegex, value) + ' kr'; // Ensure ' kr' is always added
}

var formatMoney = (val => formatMoney$1(val, window.theme.moneyFormat || "${{amount_no_decimals}}"));

 

onOptionChange(_ref2) {
let {
dataset: {
variant
},
srcElement
} = _ref2;

const optionParentWrapper = srcElement.closest(selectors$G.productOption);
const optionLabel = n$2(selectors$G.optionLabelValue, optionParentWrapper);
if (optionLabel) {
optionLabel.textContent = srcElement.value;
}

const buyButtonEls = t$2(selectors$G.addToCart, this.container);
const priceWrapper = n$2(selectors$G.priceWrapper, this.container);
priceWrapper && l(priceWrapper, "hide", !variant);

// Ensure the price is formatted correctly
const defaultProductTemplate = this.isFullProduct === "true" ? true : false;
updatePrices(this.container, variant, defaultProductTemplate);