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

xandervds
Shopify Partner
10 0 1

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"}

Replies 4 (4)

Developer-G
Shopify Partner
3079 604 859

Try this

 
{% assign parsemetafield = your_metafield_key | json_parse %}

{% if parsemetafield %}
  {% assign amount = parsemetafield.amount %}
  {% assign currencyCode = parsemetafield.currency_code %}
  <p>Amount: {{ amount }}</p>
  <p>Currency Code: {{ currencyCode }}</p> 
{% endif %}

Replace your_metafield_key with the actual value of your metafield.

- If helpful then please Like and Accept Solution.
- Want to modify or custom changes or bug fix on store Hire me.
- Email: guleriathakur43@gmail.com - Skype: navrocks1 ,
- Try GEMPAGES a great page builder
-Advance Search Filter
xandervds
Shopify Partner
10 0 1

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.

xandervds
Shopify Partner
10 0 1

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

jordystoj
Shopify Partner
3 1 2

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