Accessing products that reference a metaobject in theme code

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

4 Likes

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!

1 Like

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
2 Likes

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

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!

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

Example, shop by “brand”. “Brand” being a metafield linked to the “Brands” metaobject. “Brand” page (metaobject template) will list all products from the linked brand.

  1. create metaobject, example: “brands” with brand name and other fields you may want to use - enable metaobject to be accessed from storefront and to create template;

  2. create product metafield “brand” referring to metaobject “brands”;

  3. create a snipped to show product grid with the “brand” metafield (ex: brand-product-grid.liquid) - code below pulls all the products (collection “shop-all”) and validates if the product metafield value is the same as from metaobject, building the grid. I only created a list of the product titles but you can render the “card-product.liquid” with all the accepted parameters;

{% for product in collections['shop-all'].products %}
  {% assign producthandle = product.metafields.product.brand.value.system.handle %}
  {% assign brandname = metaobject.brand_name %}
  {% assign thisbrand = metaobject.system.handle %}
  {% if producthandle == thisbrand %}
    - {{ product.title }} -
        {{ brandname | link_to: product.metafields.product.brand.value.system.url }}
    

  {% endif %}
{% endfor %}
  1. edit code in the metaobject template file created, (metaobject/brands.json), to load the grid.

Hope it is helpful. If not for you, for someone!