How to include metafield of a collection in product page

Hello, I try to explain my need. I want to include in the product page the metafield collection of that product.

Each product have a product type that matches his collection, so if there is a way to find the collection of the product, I can easy manage it. For example collection.metafields.custom.matefieldname[product.type], sorry I don’t know the syntax

Since a product could be contained in multiple collections how are you determining what collection to reference?

When you’ve got a url like this…

https://some-site.com/collections/some-collection/products/some-product

the product will be able to reference a collection. For the moment I’ll assume that’s the scenario we’re talking about.

You’re quite close to the solution so you may have already solved it. One bit of advice would be to turn the product type in a handle first. It’s possible you’ll add a type one day with a special character that’s not support in the metafield key naming convention.

This code would work and assumes as an example that:

  1. The collection is called women’s dresses (the handle will be womens-dresses)
  2. the metafield have the namespace of custom (or whatever you need to use)
  3. and the key of “womens-dresses”
{%  if collection %}
  {%  assign productType = product.type | handle %}
  {{  collection.metafields.custom[productType] }} 
{%  endif %}

Come back if you have follow up questions.

Hello dear, thank you for your help, but no unfortunately it doesn’t work. I try to explain better:

  1. The product is not contained in multiple collections.
  2. The product for sure is in one collection named like it’s product type.
  3. The url of the product doesn’t contain the collection, is like:
https://some-site.com/products/some-product

I’ll give you an example to better explain my question:

  1. in the collection “Plates” I have the metafield: cagtegory_1 = “Table”.
  2. In the product page x, where product type = “Plates” (then equal to the collection name at point 1), I want to show the value “Table”.