Metaobjects - filtering by product field within the object * Help needed pls! *

Metaobjects - filtering by product field within the object * Help needed pls! *

jim00
New Member
9 0 0

Hi,

Hope this is the right place to be asking for help with Liquid code?!

 

I have created a metaobject = my_metaobject, one of the fields within that metaobject is called "main_product_featured" of type product.

Metaobjects have been created and the product selected for this field.

 

Where I have a product, I want to be able to select the metaobjects that have "main_product_featured" = this.product, then i'll loop through the metaobjects outputting the extra info.

 

So I'm trying the following - but "asSeenIn" is empty:

 

{% assign asSeenIn = shop.metaobjects.my_metaobject.values | where: "main_product_featured",  product %}

What am I doing wrong?

 

Many thanks in advance for your help!

 

Replies 6 (6)
jim00
New Member
9 0 0

Hi Williams4 - thanks so much for taking the time to reply.

 

What you  suggest makes sense to me, but the where filter is still not working for me with the product.handle.

 

If I remove the filter and output asSeenOn to just display the contents of one of the MetaObject that I am trying to filter in the metaobjects collection:

{% for metaobject in asSeenIn %}
    <p>{{ metaobject | json }}</p>
{% endfor %}

I can see that main_product_featured product object is in the form gid://shopify/Product/88888888 - eg is a product object - but that doesn't seem to work as a match with product.handle (or product.id ....or even a string that matches the gid://blah in the object).

 

What I think I need to do is specify which parts of the object I am trying to match:

 {% assign asSeenIn = shop.metaobjects.my_metaobject.values | where: 'main_product_featured.handle', product.handle %}

but sadly that doesn't work either...am out of ideas and feel I'm missing something here!

 

If you have any further suggestions that would be great?!

jim00
New Member
9 0 0

Thanks again Williams4 - that sounds perfect - but unfortunately the "where_eq" filter doesn't do anything for me, the code editor say "unknown filter", and I can't find it in the docs either - is there a typo?!

 

I can get round it by looping through all the metaobjects and filtering with an "if" statement, but this seems inefficient and will go wrong when there are more the 50 objects if i've understood correctly that is the max that Shopify will load in one collection?

JonoNZ
Shopify Partner
13 0 8

Hey Jim00, just found myself in exactly your position. Unable to use "where" to filter a referenced object. Can't use nested values, can't even match the GID with a string.

 

Did you resolve this?

jim00
New Member
9 0 0

Hi JonoNZ

 

Sadly I've not got anywhere further with it - feels like a bug or a significant oversight to me - and I've not found anywhere to get any real support with this sort of issue either (see this similar issue I have where "sort" filter doesn't work on int|dates here where I've failed to get any response).

 

If you do have any luck getting it working ....or if Shopify add a filter like the "where_eq" Wiliams4 appears to have 'hallucinated' above, then hopefully they'll update this thread to let us know. 

 

The only work around I have is to loop through all the metaobjects and match on the handle with an "If" statement, which whilst ugly, works ok - but will break when there are more than 50 metaobject (see "for" docs https://shopify.dev/docs/api/liquid/tags/iteration-tags max 50 iterations in For loop) - so is not a long term solution for me.

 

Good luck - and let me know if you get it working please! 

 

 

JonoNZ
Shopify Partner
13 0 8

Thanks for the update Jim00, I'll certainly update this thread if I make a breakthrough.

 

Seems like we must be missing something, perhaps going about this the wrong way. I can't imagine they would release this powerful tool with no way to filter the results.

 

Keep in touch!

jim00
New Member
9 0 0

Indeed, fingers crossed someone will point us the right direction soon ...until then I'll just hope my user doesn't add more than 50 metaobjects!