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 %}
{% 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)