How to return a currency object in liquid

I can see in the Liquid reference that there is a “Currency” object with a “Symbol” attribute

The example shows that this code:

Symbol: {{ currency.symbol }}

should produce this output:

Symbol: $ 

How can I retrieve the symbol property of “shop.currency” ?

I tried shop.currency.symbol with no luck.

How can I use the symbol attribute on the shop currency if it return the ISO value as a string?

2 Likes

Hi @Giulio1

It’s pretty disappointing that the currency object is inaccessible even though it is stated in the docs.

I was able to get this to work by accessing shop.money_format and then i used substring to extract the currency symbol from the result.

It’s a bit of an hack but i guess it works (:

Here is my code:

let shop_symbol = "{{ shop.money_format }}";
  shop_symbol = shop_symbol.trim();
  if (shop_symbol.substring(shop_symbol.length - 1) !== '}' && shop_symbol.substring(shop_symbol.length - 1).length > 0) {
  	shop_symbol = shop_symbol.substring(shop_symbol.length - 1);
  } else {
    shop_symbol = shop_symbol.substring(0, 1);
  }

Alternatively, which is way better, you can create a function that just replaces the variables, with your product price. You can find all the possible currency variables in here: https://help.shopify.com/en/manual/payments/currency-formatting

1 Like

Thank you for this solution, @mr-easy . It’s a good work-around.

Here’s my version, in Liquid:

{% capture currencyString %}{{ shop.money_format }}{% endcapture %}
{% assign tempArray = currencyString | split: ‘{’ %}
{% assign currencySymbol = tempArray.first %}

{{ currencySymbol }}

I’ve only tested it for one currency, however, as that’s all my shop is set up to serve.

You can do this a couple different ways in liquid. The simplest is:

{{ cart.currency.symbol }}

You can also do this. It will only run once if you only have one currency enabled.

{% for currency in shop.enabled_currencies %}
  {{ currency.symbol }}
{% endfor %}

If you have more than one and you’re just looking for the shop’s primary/default currency, you can add a quick check like this:

{% for currency in shop.enabled_currencies %}
  {% if currency.iso_code == shop.currency %}
    {{ currency.symbol }}
    {% break %}
  {% endif %}
{% endfor %}
7 Likes

Thanks, Gina. That’s a much better solution than mine.

This is returning the wrong symbol for some countries.

Egyptian pound (EGP) will return “ج.م” instead of “LE”

{{ cart.currency.symbol }}