Access currency rate from Liquid

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

1 Like

You could store them in cart attributes and remove them when you no longer need to use them.

:wave: there is a liquid drop for currency – does this work for your use case?

Doesn’t look like to have currency rate

This is what I need:

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 :man_shrugging:

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.