Metafield list access

Hello all,

I’d like to use a list and extract a value from a list metafield.
For now, I’m using the following metafields:
collection title1 (single-line text)
collection url1 (url)
collection title2 (single-line text)
collection url2 (single-line text)

In my product at the product metafield “test multi title” i have:

Cats & Dogs
Cat Breeds

But I’d like to use a collection metafield with a list of values ​​(to retrieve the collection name). How can I retrieve a specific value from a list?

My code that I would like to make simpler is as follow:

{% if product.metafields.custom.test_collection_title != blank %}
  {{ product.metafields.custom.test_collection_title1.value | link_to: product.metafields.custom.test_collection_url1.value }}
  {{ divider_html }}
  {{ product.metafields.custom.test_collection_title2.value | link_to: product.metafields.custom.unsen_collection_url2.value }}
  {{ divider_html }}
  {{ product.metafields.custom.test_collection_title3.value | link_to: product.metafields.custom.unsen_collection_url3.value }}
{%- endif -%}

Thank you!

Hi @Wildwolf,

This is still not clear enough for me to check.

Can I send you a collaborator invitation? It will help me check things better.

hi,

instead of using multiple fields, I would use a variable.

test_field1, test_field2,… would become test_field[0], test_field[1] but i dont know how to get the test_field[0] value..

oups…

my bad, i was using multi line instead of single line with multi value…

seem to be ok, but didn’t check…need to go to work :wink:

found it…with a hack here with the filter compact

I use metafield text with single line text but with list of values, then i use

{% assign my_product = product.metafields.custom.product_list.value | compact %}

{% assign my_product_url = product.metafields.custom.product_list_url.value | compact %}

{{ my_product[0] | link_to: my_product_url[0] }}

Regards

Hi @Wildwolf,

So did it work?

Yes thank you, it did :slight_smile: