Accessing products that reference a metaobject in theme code

CamDWH
Visitor
1 0 3

I'm exploring the new metaobjects feature and it's opened up a world of flexibility! However I have a question for the community to see if anyone knows if the following is possible...


When viewing a metaobject entry in the shopify admin there is a section called "Referenced by" that lists all products (and other entries) that reference this metaobject in their own metafields, and I'm wondering if it's possible to access these back referenced products via code in the theme files?


My use case is as follows:

 

  1. Create a metaobject definition called "Product Group"
  2. Create a Product Group entry called Group 1
  3. Create a metafield on products that references the Product Group metaobject
  4. Assign Group 1 to Product X's metafield, repeat this for Products Y and Z
  5. Access Products Y and Z via Group 1's back references from Product X


This is what I imagine the code might look like:

 

assign group = product.metafields.custom.product_group.value
assign group_products = group.referenced_by !!this is the part I'm interested in!!

Does anyone know if there is a way to do this? For the time being I've decided to add an extra metafield to the metaobject called products and I manually add the products so that they can be accessed directly via bundle.products.value, however this adds a bit of duplicated work that I'd ideally like to avoid, and since the admin seems to be able to display these back references I'm hoping the code can as well.


Cheers,


Cam

Replies 3 (3)

lesterdefreitas
Shopify Expert
24 0 9

Hi @CamDWH 

 

I'm also really interested in this. I've tried a few methods to try and achieve this through some complex forloops but even if I found a solution it would be highly inefficient and would probably slow down the page rendering a lot. So to be able to access the referenced_by values from a given metaobject would be huge.

 

My gut instinct is that it is not currently possible. But hopefully it can be added to the liquid API at some point!

Lester de Freitas
The Web Elite | Shopify Experts

RobFarmLink
Shopify Partner
37 2 26

This can be achieved by using storefront filtering.

 

  1. Install Search & Discovery app
  2. Add your product metafield (the one pointing to your metaobject) as an available filter
  3. Use storefront filtering URL query parameters either in a link or by loading using JavaScript via the Section Rendering API
  4. The query parameter would look like this:
filter.p.m.YOUR_METAFIELD_NAMESPACE.YOUR_METAFIELD_KEY=METAOBJECT_GLOBAL_ID_URL_ENCODED

for example, my namespace is farmlink, metafield is producer, and gid://shopify/Metaobject/52723384482 is the ID of the metaobject:

/collections/all?filter.p.m.farmlink.producer=gid%3A%2F%2Fshopify%2FMetaobject%2F52723384482

 

 

lesterdefreitas
Shopify Expert
24 0 9

That sounds like it would work, thank you for sharing that! I will definitely try that out next time I run into this.

Lester de Freitas
The Web Elite | Shopify Experts