How can I access collection metafields from product (belonging to that collection) page?

Like the title says, how can I access the metafields for a collection from a product page where the product belongs to this collection? I would be a neat way of inheriting metadata from broader collections instead of writing the same metadata value for each product in that collection.

And a follow up question. Can I have collections that are purely for grouping products, and not visible in the shop?

Metafields are like the child objects for main resource types: products, variants, collections, customers, orders etc. Each resource type (products and collections, in our case) has its very own set of metafields that can be accessed in context of the given resource type only.

Here’s a workaround that should help you. Try to loop through the list of the product’s collections and get the metafields from there:


 {% for collection in product.collections %}
  **Collection title:** {{ collection.title }} **Metafield value:** {{ collection.metafields.my_fields.single_text_select.value }}

 {% endfor %}
  

 

Don’t forget to replace the

collection.metafields.my_fields.single_text_select.value

part of the code with the proper Liquid code for your collection’s metafield.

2 Likes

Thank you so much! That’s exactly what I want to do!

1 Like