How to return a currency object in liquid

How to return a currency object in liquid

Giulio1
Excursionist
31 3 7

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?

Replies 6 (6)

mr-easy
Shopify Partner
10 0 3

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

 

 

Ross_Angus
Shopify Partner
15 0 5

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 %}
<h2>{{ currencySymbol }}</h2>

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

gina-gregory
Shopify Partner
742 51 213

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 %}
Ross_Angus
Shopify Partner
15 0 5

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

ScalaApps
Shopify Partner
48 0 8

This is returning the wrong symbol for some countries.

 

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

Scala Apps



- If you find my reply helpful, please hit Like and Mark as Solution

- Se você achar minha resposta útil, por favor clique em Curtir e Marcar como Solução

Parcelamento/Installments | Hide sold variants | Apps | Blog

josegomezmottan
Shopify Partner
3 0 0

{{ cart.currency.symbol }}