Re: Accessing products that reference a metaobject in theme code

Accessing products that reference a metaobject in theme code

CamDWH
Visitor
1 0 4

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 5 (5)

lesterdefreitas
Shopify Partner
30 0 17

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
44 2 47

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 Partner
30 0 17

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

KatjaL
Visitor
1 0 0

It's been a wile since this was posted, however this is exactly what im searching for:

-    When viewing a metaobject entry in the shopify admin there is a section called "Referenced by" that lists all products

Has there been any update on this? I load metaobject of selected type (Color Variations) in liquid template, however i am just not able to access it's references. 

Any solutions aroud?
Thanks in advance!

lindahlcorp
Shopify Partner
9 0 2

I'd also love to see this supported in the liquid. I've been digging around and can't find a solution even by looping over `shop.metaobjects.` etc

Freelance Shopify Developer & Ecommerce Operator