metafield

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