How to access data in a custom data metafield of type 'money'

I’m working on a theme for my store. We added in a javascript-based functionality that allows people to calculate a quick estimate for a product, outside of actually ordering this. It works with volume pricing, for which I added custom data metafields of the type ‘money’ on products. I’d like to access the data in these fields from my liquid template. When I address the custom metafield, it returns a JSON-like string, like below. I’d like to specifically address the amount or currency. When looking further, documentation and shopify help told me to address these by adding the .amount or .currency_code key in my liquid. But this does not work. How can I get to the data in this json-object without having to use JS and process it on the frontend?

{“amount”:“23.6”,“currency_code”:“EUR”}

Try this

{% assign parsemetafield = your_metafield_key | json_parse %}

{% if parsemetafield %}
  {% assign amount = parsemetafield.amount %}
  {% assign currencyCode = parsemetafield.currency_code %}
  

Amount: {{ amount }}

  

Currency Code: {{ currencyCode }}

 
{% endif %}

Replace your_metafield_key with the actual value of your metafield.

Hi Monicadanvers33, thanks for the help. I’ve tried this approach, but it also outputs nothing. I’m sure the values are set correctly, and if I output the entire metafield value it shows me the json string, so I know that the data is in there. It just doesn’t return anything when trying to parse the json…

It does show the ‘Amount:’ and ‘Currency:’ so it is just the actual amount and curreny_code that return nothing

Hi Guleria, thanks for the help, but this didn’t work. The json_parse filter doesn’t exist in my liquid (it was showing an underlined error in the editor for it), so this also doesn’t return anything for the ‘amount’ and ‘currency_code’ values.

You need to make use of the metafield_text filter available in Liquid

{% assign money_metafield = product.metafields.namespace.key | metafield_text %}
// Output of money_metafield = €23.6 EUR

Hope that helps

But If I only want the 23.6 and not currency code

{% assign money_metafield = card_product.metafields.custom.money_metafield.value %}
{{ money_metafield |  money_with_currency }}

Try this

{% assign money_metafield = card_product.metafields.custom.money_metafield.value %}

{{ money_metafield |  money_without_currency}} 

You need to add the ‘value’ metafield filter and then use one of the money filters to render the value in your preferred format.

Example:

{% assign suggested_retail_price = card_product.metafields.custom.suggested_retail_price.value %}

{{ suggested_retail_price |  money_with_currency }}

{{ suggested_retail_price |  money }}

{{ suggested_retail_price |  money_without_currency}}

1 Like