How can I display computed Metafield in my product code?

Hi,

I have the following code

{%- assign each_ingredients = product.metafields.product.ingredient_list | split: “|” -%}
{%- for ing in each_ingredients -%}
{{ ‘shop.metafields.product_ingredients.product_ingredients_’ | append: ing -}}
{% endfor %}

Say for instance ing is equal to magnesium I want the metafield shop.metafields.product_ingredients.product_ingredients_magnesium to be displayed. Instead the above code simply outputs the string metafield shop.metafields.product_ingredients.product_ingredients_magnesium. Any ideas?

Hi @molten_frog

that code you implemented prints 2 strings concatenated, and it behaves correctly.

Can you try something like this?

{%- assign each_ingredients = product.metafields.product.ingredient_list | split: "|" -%}
{%- for ing in each_ingredients -%}
    {%- capture n_ing -%}
        {{ 'shop.metafields.product_ingredients.product_ingredients_' | append: ing }}
    {%- endcapture -%}
    {{ n_ing }}
{%- endfor -%}

Thanks @drakedev I have tried this myself. Unfortunately it does the same as my original code :disappointed_face:

Hi @molten_frog ,

You can try this code:

{%- assign each_ingredients = product.metafields.product.ingredient_list | split: "|" -%}
{%- for ing in each_ingredients -%}
        {%- capture texting -%}product_ingredients_{{ product_ingredients_ }}{%- endcapture -%}
        {{ shop.metafields.product_ingredients[texting] }}
{% endfor %}

Hope my answer is clear to you. Please mark it as a solution if you find it helpful.

Hey @LitExtension this worked great. Only slight change was

{%- capture texting -%}product_ingredients_{{ ing }}{%- endcapture -%}

Thanks very much for that. I will mark it as a solution, but if you could make the change so that anyone else looking doesn’t get confused :slightly_smiling_face: