Solved

How can I display computed Metafield in my product code?

molten_frog
Tourist
10 0 1

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?
Accepted Solution (1)

LitExtension
Shopify Partner
4860 1002 1141

This is an accepted solution.

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.

 

 

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify

View solution in original post

Replies 4 (4)

drakedev
Shopify Partner
685 148 231

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 -%}
If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.
molten_frog
Tourist
10 0 1

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

LitExtension
Shopify Partner
4860 1002 1141

This is an accepted solution.

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.

 

 

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
molten_frog
Tourist
10 0 1

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 🙂