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

Solved

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

xandervds
Shopify Partner
15 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"}

Accepted Solution (1)

zenvbella
Shopify Partner
1 1 1

This is an accepted solution.

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

<span>{{ suggested_retail_price |  money_with_currency }}</span><br>
<span>{{ suggested_retail_price |  money }}</span><br>
<span>{{ suggested_retail_price |  money_without_currency}}</span><br>

View solution in original post

Replies 8 (8)

Guleria
Shopify Partner
3576 723 1007

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.

- Drop an email   if you are looking for quick fix or any customization
- Email: guleriathakur43@gmail.com
- Try GEMPAGES a great page builder
- If you want to help me please PAYPAL
xandervds
Shopify Partner
15 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
15 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
4 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

manpreet_9
Shopify Partner
1 0 0

But If I only want the 23.6 and not currency code 

scenWebDev
Shopify Partner
3 0 0

Try this

 

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

<span>{{ money_metafield |  money_without_currency}}</span> <br>

 

scenWebDev
Shopify Partner
3 0 0

 

 

 

{% assign money_metafield = card_product.metafields.custom.money_metafield.value %}
<span>{{ money_metafield |  money_with_currency }}</span><br>

 

 

 

zenvbella
Shopify Partner
1 1 1

This is an accepted solution.

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

<span>{{ suggested_retail_price |  money_with_currency }}</span><br>
<span>{{ suggested_retail_price |  money }}</span><br>
<span>{{ suggested_retail_price |  money_without_currency}}</span><br>