1
votes

I need to display n number 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 earring like:
product.metafields.earring-1
product.metafields.earring-2

but when i am trying to read value of metafield earring.name, i am not getting any output. I think because product.metafields.earring-1 is a string.

Is there any possible way to loop through metafields like this and get values?

1
Little question: why are you using metafield instead of product images?Alice Girard
Its because of complexity of design and required effect on product images.Alok Jain
@AlokJain.. Unless you are storing the images outside Shopify's CDN, @Alice's question is spot on. Shopify's CDN has added advantages. You can always configure the images loop. Secondly, you need only use one metafield to store all image or single type of data. Added them as a string separated by an distinct character. Then use | split: usage to create and array and loop it. Reduces server load.HymnZzy

1 Answers

3
votes

Just 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 %}