How to display individual values from 'Accept list of values' in metafields?

I’m trying to take advantage of the “Accept list of values” option in the ‘Single line text’ metafield option.

If I’m understanding how it works, I should be able to access those individual values, and display them on the page.

So far all I can do is just print the values in brackets:

{{ product.metafields.my_fields.list1 }}

[“test1”,“test2”,“test3”]

I tried {{ product.metafields.my_fields.list1[1] }} but that didn’t do anything.

Any help would be appreciated.
Thanks, Nick C.

Oh do i just need to add “value”?

{{ product.metafields.my_fields.list1.value[1] }}

Did you ever figure this out? I am running into issues where this is not displaying a list where I place the metafield.

For my purposes {{ product.metafields.my_fields.list1.value[1] }} this worked.

I haven’t looked into how to make it do a loop, where it will automatically display all the items in a list, as I don’t necessarily need it to at the moment. So I’m just doing it manually.

fish, blue, apple, hat

{{ product.metafields.my_fields.list1.value[0] }}

{{ product.metafields.my_fields.list1.value[1] }}

{{ product.metafields.my_fields.list1.value[2] }}

{{ product.metafields.my_fields.list1.value[3] }}

fish

blue

apple

hat

You can simply iterate over the “value” to expose each list item:

{% for related_product in product.metafields.related.products.value %}
  ...
{% endfor %}

Or:

{% assign related_products = product.metafields.related.products.value %}
{% for related_product in related_products %}
  ....
{% endfor %}

If we set this up on products and each product might have a different number of list items, what happens if we reference a non existent value? Would it be somehow possible somehow to code an if-then to fallback to value[1] in those situations if the called list reference value doesn’t exist?

For example: {{ product.metafields.my_fields.list1.value[5] }} is used but the product’s metafield list only has 3 items… so show {{product.metafields.my_fields.list1.value[1] }} instead

yes,

{% if product.metafields.my_fields.list1.value[5] == blank %}

this will show if 5 IS empty

{% endif %}

{% if product.metafields.my_fields.list1.value[5] != blank %}

this will show if 5 is NOT empty

{% endif %}

I think you can also just use {%- else -%} but I like using 2 separate statements, if necessary.