metafield

Topic summary

A user is working with two Shopify metafields of type “multiple values”:

Metafield 1: Contains multiple product references (product1, product2, etc.)
Metafield 2: Contains multiple images (image1, image2, etc.)

Goal: Pair each image with its corresponding product by index position (image1 with product1, image2 with product2, etc.)

Solution provided: Use nested loops with forloop.index to match products and images by their position. The map filter can convert the image list to an indexed array for easier access.

Remaining issue: When a custom image is missing for a product, the code needs to fall back to the product’s default featured image. This fallback functionality has not yet been implemented in the proposed solution.

Summarized with AI on November 6. AI used: claude-sonnet-4-5-20250929.

hi i have two metafileds with type multiple values
metafield no 1 select the multiple products like product1,product2,product3,product4 etc
then i show the title and link of that product.

metafield no 2 select the multiple images like image1,image2, image3,image4 etc

know i want that image 1 is attached with product 1 and image 2 is attached with product2 and so on.

how i can achieve this:
here is the code:

{% for recommended_product in product.metafields.custom.custom_product.value %}

{{ recommended_product.title }}

{% endfor %}

in the above code i want to change the recommended_product.featured_image with my custom images that are coming from metafields
is there any solution?

According to dox https://shopify.dev/docs/api/liquid/objects#metafield-accessing-metafields-of-type-list you can only iterate over them, no direct access.

So it should be like:


  {% for recommended_product in product.metafields.custom.custom_product.value %}
    
      {{ recommended_product.title }}

      {% assign product_index = forloop.index %}
      {% for image in product.metafields.custom.custom_image.value %}
        {% if forloop.index == product_index %}
          
        {% endif %}
      {% endfor %}
    
  {% endfor %}

Which does not look like the most effective/elegant.

So here is the trick which worked for me (because for images preview_image points to themselves and map kinda converts the list to a normal indexed array)

{% assign images =  product.metafields.custom.custom_image.value | map: "preview_image" %}

  {% for recommended_product in product.metafields.custom.custom_product.value %}
    
      {{ recommended_product.title }}

      
    
  {% endfor %}

1 Like

it works fine but if there is no custom image for some product it also need to pick

feature image from product