Saba13
June 19, 2025, 11:08am
1
Good day, I hope everyone is doing well!
I wish to display the ‘related products’ on the collection page as swatches. These related products are stored as a Product List Metafield.
When I call the metafield on the collection page in for-loop for products, it is fetching only the GIDs of the products instead of the actual product object.
I even tried to loop through the GIDs and fetch the matching products but the GIDs are not looping (maybe because they’re not an array?).
I am using the exact same metafield on the product page to show the ‘related products’ and that is working absolutely correctly but it’s not fetching the current data type on the collection page. Why is that?
The only difference between the collection page and product page is that on the product page the metafield is assigned to a block.setting.
Is there a way to work around this? How do I get the Product Object in the collection page?
Here is the code:
CPL: {{ product.metafields.custom.linked_product_list }}
{% for p in product.metafields.custom.linked_product_list %}
P: {{ p }}
{% assign matching_count = matching_count | plus: 1 %}
{% capture output %}
{{ output }}
{% if p.featured_image %}
{% endif %}
{% endcapture %}
{% endfor %}
{% if matching_count > 0 %}
Colors:
{% if matching_count > 8 %}
{% endif %}
{{ output }}
{% if matching_count > 8 %}
{% endif %}
{% endif %}
hey @Saba13 share the URL of your webiste plz
Saba13
June 19, 2025, 11:34am
3
Hey Saba,
Try doing {{ p.product.title }} instead of {{ p.product }}
Code sample
{% for p in product.metafields.custom.linked_product_list %}
P: {{ p.product }}
{% assign matching_count = matching_count | plus: 1 %}
{% capture output %}
{{ output }}
{% if p.product.featured_image %}
Accessing values on a list requires an additional step.
Hope this helped!
Saba13
June 19, 2025, 12:24pm
5
Nothing changed:
CPL: {{ product.metafields.custom.linked_product_list }}
{% for p in product.metafields.custom.linked_product_list %}
P: {{ p.product.title }}
{% assign matching_count = matching_count | plus: 1 %}
{% capture output %}
{{ output }}
{% if p.featured_image %}
{% endif %}
{% endcapture %}
{% endfor %}
{% if matching_count > 0 %}
Colors:
{% if matching_count > 8 %}
{% endif %}
{{ output }}
{% if matching_count > 8 %}
{% endif %}
{% endif %}
Hey Saba,
I believe what you are seeing is this
CPL: {{ product.metafields.custom.linked_product_list }}
Which renders the entire list onto the page. Try to remove that, and try again.
Also, use “p.product” suffix on all instances of “p”
Saba13
June 19, 2025, 12:53pm
7
I removed the CPL and added the suffix as you suggested:
{% for p in product.metafields.custom.linked_product_list %}
P: {{ p.product.title }}
{% assign matching_count = matching_count | plus: 1 %}
{% capture output %}
{{ output }}
{% if p.product.featured_image %}
{% endif %}
{% endcapture %}
{% endfor %}
{% if matching_count > 0 %}
Colors:
{% if matching_count > 8 %}
{% endif %}
{{ output }}
{% if matching_count > 8 %}
{% endif %}
{% endif %}
But now it doesn’t show anything:
The inspect shows the outer div only:
Hi Saba,
Try changing this into
{% for p in product.metafields.custom.linked_product_list %}
this
{% for p in product.metafields.custom.linked_product_list.value %}
Notice the .value at the end
This should render it correctly, or at the very least, trigger the loop correctly.
Saba13
June 19, 2025, 1:05pm
9
Thank you so much!
Adding the .value is now rendering the items:
I did however also remove the .product suffix as it wasn’t working with it
{% if template.name == 'collection' %}
{% for p in product.metafields.custom.linked_product_list.value %}
P: {{ p.title }}
{% assign matching_count = matching_count | plus: 1 %}
{% capture output %}
{{ output }}
{% if p.featured_image %}
{% endif %}
{% endcapture %}
{% endfor %}
{% if matching_count > 0 %}
Colors:
{% if matching_count > 8 %}
{% endif %}
{{ output }}
{% if matching_count > 8 %}
{% endif %}
{% endif %}
I really appreciate all your help and time! Thank you once again!
Awesome! You’re welcome Saba!