Hey there,
I know we can access current storefront currency rate with Shopify.currency.rate
but is there a way to access it from Liquid?
Thanks for your help
Hey there,
I know we can access current storefront currency rate with Shopify.currency.rate
but is there a way to access it from Liquid?
Thanks for your help
You could store them in cart attributes and remove them when you no longer need to use them.
there is a liquid drop for currency â does this work for your use case?
Yeah you can get that on first page load then throw it into cart attributes and then use it in Liquid via the Section Rendering API, or pulling the page again and replacing the view, etc.
You just cannot get it on initial load via Liquid because itâs not there.
Just remember to remove from cart attributes when you no longer need to use it or it will show in the order page of the admin back-end.
Mhh I donât need all of that, I can just use JS instead but I wanted a straight solution using Liquid but maybe thereâs none
If something from the team is around, could be good to have that in the future
Makes sense to go that route. Usually people want it in Liquid to process things outside of the client-side so that is why I shared some suggestions on what to do to make that happen.
Liquid needs to be predictable so that is why randomness is not part of it. With exchange rates, I am guessing the same restriction applies so we likely will not see them natively in Liquid.
The easiest way would be to add a new metafield of type Money to the store level (custom.exchangeRate) and set the value to 1 EUR or whatever your default currency is.
Then you can use it like:
{{ shop.metafields.custom.exhangeRate.value | times: 90.00 | money }}
Where 90.00 is the value you want to convert.
It works like a charm on all the markets.
PS: if you notice rounding issues you can store 1000 EUR or whatever currency and then on the liquid you divide it by 1000 and youâll get the exchange rate with the digits as well.
I have to try this one. I was using global metafield to store the exchange rate, not a money value. Pretty smart.
I tested it, and the âmoney type metafieldâ only increases the number of digits of the currency value to make it easier to process. As a result, this method can only display values in the storeâs base currency.
I believe the questionerâs intent was to ask, âDespite the rate being publicly available in JavaScript, why canât we access it on the server side?â At least, I thought so, which is why I searched and found this post.
When âDisplay prices in local currencyâ is enabled in Markets, the server-side handles the price display switching using the exchange rate. Given this, it seems reasonable to expect that there should be an object in Liquid that allows access to the exchange rate.
The reason for insisting on server-side access is likely the same as preferring Server-Side Rendering over Client-Side Rendering.
I agree, there should be a way to do this in liquid. But then again I think since this rate could change any second this might not be optimal for Shopify to do liquid caching as well.
I apologize for the mistake in this section.
In reality, this method cannot be used because rounding up occurs.
For example, if we set 1 Japanese yen in a âmoney typeâ metafield and convert it in US dollars using Liquid, it becomes â1 dollarâ.
In this state, if you use {{ shop.metafields.custom.exhangeRate.value | times: 90.00 | money }}
, it results in â90 yen in Japanese currency display and 90 dollars in US dollar displayâ and this is wrong.
The current result of Shopify.currency.rate
is â0.006829869â, and it seems this is being rounded up.
Therefore, while it might work if the exchange rate is close to 1:1, it doesnât work well in cases where it isnât.
Using âmoney type metafieldâ method looks promising, but it also includes the currency conversion charges.
When I created the money type liquid metafield, and used it in the Market to calculate price, it appears different then what I get from the Shopify.currency.rate, I found that it (money type metafield) also includes the currency conversion charges. While Shopify.currency.rate is a pure conversion rate.
So reverse calculating the conversion rate does not work properly here.
Shopify says, the conversion changes are 2%, but 2% of which amount, the stores base currency or the marketâs currency? I believe the 2% is calculated considering the storeâs payout currency.
To easify all these cases, I think using the Shopify.currency.rate is a go.