New Shopify Certification now available: Liquid Storefronts for Theme Developers

Metaobject Product Field - how to access the data?

Solved
ginacostanzo
Shopify Partner
10 2 4

Hi, I've been working on implementing a new metaobject in my company's Shopify store called Lessons. I'm able to access the lesson.title, lesson.description,etc. However, I cannot access the featured products. In my metaobject I set up a field called featured products and set it as a product data type (list option). I've tried so many different formats of dot notation to access the products for a lesson and nothing works. Anyone figured this out that can offer some advice?

{% for product in metaobject.featured_products %} <div>{{ product.title}}</div> {% endfor %}

outputs nothing
.

Also tried accessing via the GIDs that are returned by looping through all_products and keeping the ones with matching IDs, but also displays nothing. 

Accepted Solution (1)
ginacostanzo
Shopify Partner
10 2 4

This is an accepted solution.

Okay update - I finally found a youtube video https://www.youtube.com/watch?v=eQHt2xil3Js that explains referencing products and objects in code and how the notation differs from something like simple text. Very informative video! 

View solution in original post

Replies 3 (3)
Made4uo-Ribe
Shopify Partner
4192 964 1173

Hi @ginacostanzo 

 

You need to call shop.metaobjects. Try the code below.  You can read more about metaobjects here

{% assign metaobject_products = shop.metaobjects['featured_products '].values %}

{% for product in metaobject_products %}
{{ product.title }}
{% endfor %}

 Please don't forget to Like and Mark Solution to the post that helped you. Thanks!

Please don't forget to 

Like

 and 

Mark Solution 

to the post that helped you. Thanks!
Sip, savor, and support – your

Coffee Tip

 helps us keep coding magic.❤️❤️❤️
Need a knowledgeable developer? Come and visit us at

Made4uo.com


ginacostanzo
Shopify Partner
10 2 4

Thanks for your reply. I'm calling metaobject directly because this is within a metaobject template. So my overall metaobject is called lessons and then within that one of the metafields is featured_products. 

I created a metafield template so it will generate a page for every metaobject entry (every lesson), so that is why i'm writing just metaobject and not shop.metaobjects.

When I write metaobject.title it works. 

If I write metaobject.featured_products I get back an array of gids, but haven't been able to use that in any way. And if I try to loop through the featured_products to display properties of each product, it's blank.

ginacostanzo
Shopify Partner
10 2 4

This is an accepted solution.

Okay update - I finally found a youtube video https://www.youtube.com/watch?v=eQHt2xil3Js that explains referencing products and objects in code and how the notation differs from something like simple text. Very informative video!