All things Shopify and commerce
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"}
Solved! Go to the solution
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>
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.
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.
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
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
Try this
{% assign money_metafield = card_product.metafields.custom.money_metafield.value %}
<span>{{ money_metafield | money_without_currency}}</span> <br>
{% assign money_metafield = card_product.metafields.custom.money_metafield.value %}
<span>{{ money_metafield | money_with_currency }}</span><br>
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>
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024