skip to Main Content

there is a way to display a collection metafield in the porduct page?

Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.

{{ collection.metafields.custom.metafield1 }} doesn’t work because of course I’m not in the collection page, so I need to link collection.metafields.custom.metafield1 and product.type, but I don’t know how.

Hope I was clear, thank you.

Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.

3

Answers


  1. Chosen as BEST ANSWER

    yes I understand your point, and I figure out your code and I tried to use it:

      {% assign the_collection = collections['{{ product.type }}'] %}
      {% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
      <span>{{ my_metafield.value }}</span>
    

    and I don't understand why it does not work.

    1. The product type exists and is "Piatti"
    2. The collection "Piatti" exists and has the metafield custom categoria_1

    I attach a sceenshot of the metafield in order to be more cleare.

    metafield structure


  2. Hard to understand your question, but it would probably go like this. In Liquid, every product has a type. So you would use that on the collections object to get just the collection you want, and from that, the special metafield assigned.

    {% assign the_collection = collections['{{ product.type }}'] %}
    

    That assigns some collection, assuming it exists, based on the current product type, to your variable. Now you would ask for your special metafield that may or may not exist in that collection’s metafields.

    {% assign my_metafield = the_collection.metafields.some_namespace.some_key %}
    

    And with that, you could get the value and splash it out there as you please.

    <h1> {{ my_metafield.value }} </h1>
    
    Login or Signup to reply.
  3. I solved with the following code

      {% assign collectionHandle = product.type %}      
      {% assign the_collection = collections[collectionHandle] %}
      {% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
      {{ my_metafield.value }}
    

    As you suggest it was fun to solve the problem 😀

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search