Liquid, JavaScript, themes, sales channels
We have many customer shops who are using a piece of tracking code we've created in Additional Scripts to report the prices of items bought. We currently list them out in an iframe on the order confirmation page as {{ line_item.price | money_without_currency | remove: ',' }}. This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).
We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.
Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? I don't see any great options even using the available string filters. I'm tempted to try to insert a decimal point manually or maybe even just divide the integer price value by 100 but I'm not confident that I've thought through every edge case on that.
Solved! Go to the solution
This is an accepted solution.
@dougbright wrote:... This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).
We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.
Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? ...
💣currencies are subject to foreign exchange rates, for example the difference in currency value between USD and EUR is ~0.85 a significant number.
You can be creating money that doesn't exist or losing money that does.
"it reads 120045 which is obviously incorrect" that is correct in the sense that is the TRUE value of the amount as shopify stores money values as cents.
It's also the value in EUR which is not the exchange rate value in USD.
No, there is no straightforward way, unfortunately there is no money filter currency arguments like: {{ | money:"GBP" }} , or similar
https://shopify.dev/api/liquid/filters/money-filters
https://help.shopify.com/en/manual/payments/currency-formatting
So , You may want to come at this the other direction, convert the pennies after they are rendered in javascript or when the servers receive them.
Otherwise If you must manually format you may need to slice filter or turn it into an array with split and parse it to build your custom formatter.
https://shopify.dev/api/liquid/filters/string-filters#slice
https://shopify.dev/api/liquid/filters/string-filters#split
{%- assign decimal_places = 2 -%}
{%- assign money_numbers_only = 2000 -%}
money_numbers_only {{ money_numbers_only}}
{%- assign digits_array = money_numbers_only | split: '' -%}
digits_array {{ digits_array | join: ',' }}
{%- assign whole_length = digits_array.size | minus: decimal_places %}
whole_length {{ whole_length }}
{%- assign whole_part = money_numbers_only | slice: 0, whole_length -%}
whole_part {{ whole_part }}
{%- assign decimal_part = money_numbers_only | slice: whole_length , decimal_places -%}
decimal_part {{ decimal_part }}
{%- assign currency = whole_part | append: '.' | append: decimal_part -%}
formatted {{ currency }}
I think it's a solved problem in otherplaces on the forums with some searching required to find it.
And if you have a maintenance cycle this should be flagged to be checked for the rare rare chance shopify ever changes how money amounts are stored from cents to floats.
This is an accepted solution.
@dougbright wrote:... This provides the format our servers need to read these values (for example, $1,200.45 is formatted as 1200.45).
We're now adding support for multi-currency and are running into a problem when the presentment currency is, say, EUR. Depending on the country, the line_item.price could read €1.200,45. After the money_without_currency and remove: ',' filters it reads 120045 which is obviously incorrect.
Is there a straightforward way in Additional Scripts to get US based currency formatting with no thousands separators regardless of the presentment currency? ...
💣currencies are subject to foreign exchange rates, for example the difference in currency value between USD and EUR is ~0.85 a significant number.
You can be creating money that doesn't exist or losing money that does.
"it reads 120045 which is obviously incorrect" that is correct in the sense that is the TRUE value of the amount as shopify stores money values as cents.
It's also the value in EUR which is not the exchange rate value in USD.
No, there is no straightforward way, unfortunately there is no money filter currency arguments like: {{ | money:"GBP" }} , or similar
https://shopify.dev/api/liquid/filters/money-filters
https://help.shopify.com/en/manual/payments/currency-formatting
So , You may want to come at this the other direction, convert the pennies after they are rendered in javascript or when the servers receive them.
Otherwise If you must manually format you may need to slice filter or turn it into an array with split and parse it to build your custom formatter.
https://shopify.dev/api/liquid/filters/string-filters#slice
https://shopify.dev/api/liquid/filters/string-filters#split
{%- assign decimal_places = 2 -%}
{%- assign money_numbers_only = 2000 -%}
money_numbers_only {{ money_numbers_only}}
{%- assign digits_array = money_numbers_only | split: '' -%}
digits_array {{ digits_array | join: ',' }}
{%- assign whole_length = digits_array.size | minus: decimal_places %}
whole_length {{ whole_length }}
{%- assign whole_part = money_numbers_only | slice: 0, whole_length -%}
whole_part {{ whole_part }}
{%- assign decimal_part = money_numbers_only | slice: whole_length , decimal_places -%}
decimal_part {{ decimal_part }}
{%- assign currency = whole_part | append: '.' | append: decimal_part -%}
formatted {{ currency }}
I think it's a solved problem in otherplaces on the forums with some searching required to find it.
And if you have a maintenance cycle this should be flagged to be checked for the rare rare chance shopify ever changes how money amounts are stored from cents to floats.
Great, Paul - thanks for the advice. I'm going to give formatting the string manually as you suggest a shot. If that fails we may have to get clever on the server.
Btw, using the same amount for USD vs EUR there was for illustration only. We are aware that the values are different and we handle them appropriately.
User | RANK |
---|---|
33 | |
27 | |
18 | |
9 | |
8 |
We're excited to announce improvements to the threaded messaging experience in our communi...
By TyW May 31, 2023Thank you to everyone who participated in our AMA with Klaviyo. It was great to see so man...
By Jacqui May 30, 2023Photo by Marco Verch Sales channels on Shopify are various platforms where you can sell...
By Ollie May 25, 2023