Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Metaobject Product Field - how to access the data?

Solved

Metaobject Product Field - how to access the data?

ginacostanzo
Shopify Partner
13 2 9

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
13 2 9

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
10202 2422 3067

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!

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
ginacostanzo
Shopify Partner
13 2 9

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
13 2 9

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!