Accessing Metaobject product references in Liquid

Accessing Metaobject product references in Liquid

JayBuys
Visitor
1 0 1
  1. I created a Metaobject named 'Artist' to house information about the artist who designed a product
  2. I've added this Metaobject to the 'Product' object
  3. I've added some Liquid code to the product template...
    {%- assign artist = product.metafields.custom.artist.value -%}
    Created by <a href="{{ artist.system.url }}">{{ artist.name }}</a>
  4. This links to an artist template that has custom Liquid code to display the artist's name, bio, photo, etc.

Is there a way to get a list of all of the products where this artist is referenced using Liquid in the artist page template? 

 

I'm currently doing it by manually creating a collection for each artist and using a collection metafield on the artist object but it seems like there should be a more dynamic way.

Replies 8 (8)

lojainusual
Tourist
5 0 2

I'm having the same issue. We have a 'Designer' metaobject - same as your artist.

And now with the metaobject pages we are trying to display all products from that designer.

 

The only way we could accomplish this is to create a collection with the designer's product. Or manually add the products on the metaobject.

 

But it does not make sense. Because we already referenced this designer to a bunch of products. We are having a double workload to manually assign the products again.

 

If we could access the referenced products on the metaobject, we could just render them on a list or a section.

 

 

prateeksha
Shopify Partner
10 2 3

I have the same issue. Have you been able to find a solution?

The fact that the meta object in shopify admin shows a list of references at the bottom ideally means that the system is wired to have this back linking. So, the question is if we can access it as a developer.

lojainusual
Tourist
5 0 2

Actually I found an answer for this case. I had the help of this tutorial https://www.youtube.com/watch?v=Rp4kVN0XivQ&t=792s

And he helped me to achieve the results.

 

Basically, we need to get the handle of the metaobject and the product metafield and browse through all the products in the store.

I was not achieving it because Shopify limits the products on a query. If we serach on a category with few products it works and gets the products. But on a category with much more, or the entire store, it wouldn't work.

 

But he pointed me to a hack: I had to paginate all the results.

 

               {% paginate collections["all"].products by 3000 %}
                {%- for product in collections.all.products -%}
                  {% if product.metafields.custom.designer.value.system.handle == metaobject.system.handle %}
                  {% comment %}Code Here {% endcomment %}
                {%- endfor -%}
                {% endpaginate %}

This is used on the Metaobject Page, so it gets the specific Metobject properties to call (metaobject.system.handle). If you use in other pages you might need to change.

 

Here is the result on our Designers: https://www.inusual.com.br/pages/designer

If you click on each you will see the products for each one. If doesn't appear is because we did not link yet.

 

Hope this will help you guys.

And hope Shopify will implement an easier way to call the referenced products.

 

prateeksha
Shopify Partner
10 2 3

Thanks @lojainusual . Yes, I ended up implementing it finally with pagination too. But in my case, I'm referring to a list of internal metaobjects called 'Impact' where I have one Impact created per order. So, eventually, this number is going to really grow and paginating through all impacts won't scale. Hence my worry. 

lojainusual
Tourist
5 0 2

Yeah.

Just an update:

I am not being able to list all products. Some products are still not appearing. A designer that has 12 products, only 6 are showing. Another that has 1 product, this one is not showing as well.

I believe it is because of the limits that Shopify has. I don't know if we are going to solve this using this method.

 

I can put them on a smaller category, assign a tag to the product or think of another way to filter them. But still it is another process to implement on the store and losing time doing it. And hoping that our employees will do it alright on all products.

 

Just hoping that Shopify will make it easier to link the products to the metaobject.

Cherb20
Shopify Partner
6 2 3

Check out this youtube video https://www.youtube.com/watch?v=wArIeBczLrI&ab_channel=CodewithChristheFreelancer
He creates something very similar to what you are needing. Hope that helps. 

If I answered your question, please Accept my answer as the solution 🙂
If you found any information I've provided helpful, please give a Thumbs Up

If you have any questions, feel free to reach out or message me.
prateeksha
Shopify Partner
10 2 3

Yes, this achieves the same effect. But, the problem with this approach is that you have to duplicate the same information in two places. 

In, product, you have to add a 'creators' field, and in the Creator object, you have to create a 'products' field. Essentially, you have to enter the same information in two places, which seems like a bad design to me. If I miss entering it in one place, the system becomes inconsistent. 

lojainusual
Tourist
5 0 2

Exactly.

 

I had a workaround using the Flow system and using a smaller category, with only the products that have a designer assigned.

To not having to do it manually (doubled work), I create an automated category and used the Flow app to put a tag on these products.

 

Whenever a product is updated, it looks if a designer is assigned to that product.

This way the category is always in sync and I can retrieve these products with liquid.