- This topic is empty.
- AuthorPosts
-
October 21, 2016 at 7:28 am #9612
abctest483MemberI need to display
nnumber of images for a product in shopify.I have stored number of images in a metafields and created a loop for it.
Then each image’s name is stored in a metafield, which i am trying to get with help of loop.
{% assign earrings = product.metafields.earrings %} {% for i in (1..earrings.total-earrings) %} {% assign earring = 'product.metafields.earring-' | append:i %} {{ earring.name }} {% endfor %}This loop is giving me values for
earringlike:
product.metafields.earring-1
product.metafields.earring-2but when i am trying to read value of metafield
earring.name, i am not getting any output. I think becauseproduct.metafields.earring-1is a string.Is there any possible way to loop through metafields like this and get values?
October 21, 2016 at 7:49 am #9614
abctest483MemberJust in case it’s helpful for someone.
Here’s the updated code:
{% assign earrings = product.metafields.earrings %} {% for i in (1..earrings.total-earrings) %} {% assign dummy = 'earring-' | append:i %} {% assign earring = product.metafields[dummy] %} {{ earring.name }} {% endfor %}December 8, 2023 at 7:17 am #9613
abctest483MemberSo the simple way to loop through all the metafields is here:
CONDITIONS To loop through metafields, you should keep the same namespace, by default Shopify sets "custom" namespace to all metafields. You can keep anything while creating a metafield. Let’s assume we will loop through all the metafields with the namespace added as "option", so we have
product.metafields.option.KEY.{% for metafield in product.metafields.option %} {% assign key = metafield[0] %} {% assign type = product.metafields.option[key].type %} <!--// Now you got the meta-field's KEY & TYPE, handle it according to its access syntax; for example below, I am printing text of type text-multiline(list.single_line_text_field) meta-field values --> {% assign values = product.metafields.option[key].value %} {% for value in values %} {{ value }} {% endfor %} {% endfor %} - AuthorPosts
- You must be logged in to reply to this topic.