How do I access collection values from a GID in a template?

Solved

How do I access collection values from a GID in a template?

RetroShirtsMan
Excursionist
24 1 8

Hi,

 

I can't find the proper way to call / load the collection object from a GID. 

 

I am trying to access collection values (Title / Metafields) in a template.

All I have available is a custom collection reference field:

 

product.metafields["details"]["mycollectionfield"]

containing a gid value:

 

gid://shopify/Collection/111111

And i would like to use this referenced collection in an object to access the values of the collection like Titles / Images and such.

 

I found another similar thread about a the product object. The solution said, it is required to assign the object to a variable, but I have not found the example code to do that with a collection reference metafield.

 

Would be great to be pointed to the documentation explaining the steps.

Accepted Solution (1)

kaalTechGeeks
Shopify Partner
311 57 78

This is an accepted solution.

Hi @RetroShirtsMan
You can access the collection saved in a product meta field by first assigning the collection in a variable as shown in below code: 

{% assign mycollection = product.metafields["details"]["mycollectionfield"].value %}
{% comment %} 
  {{ mycollection.title }} - returns collection title  
  {{ mycollection.url }} - returns collection URL 
{% endcomment %}

 Please let me know if you need more help 
Thanks

- If my answers saved you time and headaches, consider buying me a coffee!
- Check my profile: KaalTechGeeks | Reach out to me on my socials - Instagram | Skype | Mail
- Was my reply helpful? Click Like to let me know! & if your question was answered, Mark it as an Accepted Solution!

View solution in original post

Replies 4 (4)

kaalTechGeeks
Shopify Partner
311 57 78

This is an accepted solution.

Hi @RetroShirtsMan
You can access the collection saved in a product meta field by first assigning the collection in a variable as shown in below code: 

{% assign mycollection = product.metafields["details"]["mycollectionfield"].value %}
{% comment %} 
  {{ mycollection.title }} - returns collection title  
  {{ mycollection.url }} - returns collection URL 
{% endcomment %}

 Please let me know if you need more help 
Thanks

- If my answers saved you time and headaches, consider buying me a coffee!
- Check my profile: KaalTechGeeks | Reach out to me on my socials - Instagram | Skype | Mail
- Was my reply helpful? Click Like to let me know! & if your question was answered, Mark it as an Accepted Solution!
RetroShirtsMan
Excursionist
24 1 8

It works thank you very much. I will mark it as solved in a minute.

 

But might you provide me with the link to the documentation where I could have looked or found this myself? I tried hard and was not able to find it.

kaalTechGeeks
Shopify Partner
311 57 78

Hey @RetroShirtsMan, I am glad that my solution worked for you. 
Also you can refer to the below doc in order to explore more about metafields: https://shopify.dev/apps/metafields?shpxid=ecfb75a2-D7B0-43D5-F156-3616555F0B9F 

Thanks

- If my answers saved you time and headaches, consider buying me a coffee!
- Check my profile: KaalTechGeeks | Reach out to me on my socials - Instagram | Skype | Mail
- Was my reply helpful? Click Like to let me know! & if your question was answered, Mark it as an Accepted Solution!
JohnFromMurdock
Tourist
4 0 8

Thanks so much for this information on GID! I did not know it was an object like this!