I’m working with theme customisation and liquid. Wondering how best to get all products which belong to a metaobject , in this case I have books (product) which belong to a publisher (metaobject).
I already have the publisher page - now I want to display all their books.
I thought I could access and loop through the products once I had the metaobject: {% for publisher in publishers.publisher.value %}, but there’s nothing in the docs about the reference or relationship to products.
Chat GPT came up with a suggestion of looping through all products and using if product.publiser === 'my-publisher' to access the ones I wanted, but the idea of looping through the entire product catalogue seemed wildly inefficient.
I can create collections and perhaps use those but these requires manual creation for each publisher.
Is there a more elegant solution? Or something i’m missing from the docs?
Unfortunately, you can’t retrieve a list of products based on a metaobject value filter. Additionally, you should not loop through all products and match values manually, as this can lead to issues with Liquid due to its maximum iteration limit of 50.
I considered using the Storefront API and applying a query filter to retrieve products with specific metafield values. However, this doesn’t seem possible based on the search syntax docs.
My recommendation would be to create collections that contain the relevant products or use a corresponding product metafield to reference the publisher. You might even consider using metaobjects for this purpose.
As for which solution to choose, it really comes down to your temperament and your skill set when it comes to automation. Some options might feel more straightforward or manageable depending on how comfortable you are with automating data population. Tools like Shopify Flow, Make, Arigato, and Mechanic are particularly useful for automating tasks like this if you’re comfortable using them.
Thanks, seems like collections might be the way to go.
A question about storefront, i’m not building my own shop, i’m using a theme and customising it myself. I was under the impression that the storefront API couldn’t be used in this scenario due to exposing private keys on clientside, and can only be used if I have my own back end. Is this correct?
It’s actually only the Admin API which uses a private keys. The Storefront API reveals a public API key which can’t be used for anything really malcious other than checking otherwise unavailable data like stock quantities.
I am jumping in here late, but just use the Metaobject containing metafielcd as a Smart Collection rule.
Use Shopify Flow for “On Metaobject Entry Created” set the Metaobject type to listen for, and then use the “Send Admin API Request” action block with the “CollectionCreate” mutation and build out the mutation data to create the collections as new entries of the metaobject are done.
The Flow part requires a little bit of technical knowledge, but I would rather set up automated collections than pulling from the Storefront API and refetching x times and handling the potential pagination and all myself.
I like the creative thinking - just to be sure; are you suggesting to create “manually” sorted collections populated by logic in a Flow? It’s actually quite smart
However; “On Metaobject Entry Created” is all good and stuff; but what would you suggest for changes? There doesnt seem to be a “On Metaobject Entry Updated”, which I guess could be a case as well.
Metaobjects can be used in smart collection rules. If the metaobject, is related to a product from a product level metafield, where that product level metafield is of type metaobject reference, and the metafield is set to be used as a collection rule.
As the metaobject is added and removed from products, the products would be added and removed from the collection automatically via the smart rules.
I would go further and add a collection field to the metaobject, and when this flow creates the collection to run another API mutation to add the collection reference to the metaobject.
Hell, set the metaobject to have published templates and you can build a full landing page for the author that list their info and uses the collection to render out their author related products.
That being said, a flow trigger for metaobject update would be good for continuity changes like maybe titles or something you want kept in sync between the metaobject and the collection. Like an authors name is changed so let’s change the associated collection name.
While I don’t use Flow for this, I have something just like this set up on metaobjects webhooks.
Ah that makes sense - I misunderstood it then. It’s the same as - but very much more elaborated than - what I ment in my first messages then. Still a good idea regarding automatically populating that product metafield with a metaobject reference. Thanks for elaborating