Is there any way to use a generic resource with the metafields object?

I have a metafield that exists across a number of page types that I use in the theme.liquid file. Instead having to write a condition for: page.metafields.namespace.key, collection.metafields.namespace.key, blog.metafields.namespace.key, etc. I want to just use a generic metafields.namespace.key.

I can get the resource with request.page_type, but I can’t find any way to connect it as a variable. I tried a number of things:

  • metafields.namespace.key
  • request.page_type.metafields.namespace.key
  • settings[request.page_type | append: “.metafields.namespace.key”]
  • metafields[request.page_type].namespace.key
  • {% capture key %}{{ request.page_type }}.metafields.namespace.key{% endcapture %} {{ key }}

None of the above works. I’m assuming this just isn’t possible, but anything I haven’t tried?

Hi @sn3 ,

product.metafields it’s an object, not a string, so you can’t declare it with request.page_type . Refer https://shopify.dev/api/liquid/objects/product#product-metafields

same for collection, page, etc

So you can just get it in the correct structure: page.metafields.namespace.key, collection.metafields.namespace.key, blog.metafields.namespace.key, etc

Hope it helps!

Hi @sn3 ,

The thing is that metafields are stored in specific instance of an object (in each specific blog, collection, page etc). In order to access metafields, one must use a proper Liquid code of the given structure:

{{ resource.metafields.namespace.key.value }}

If the “request” variable doesn’t represent nor contain the actual instance of an object, you won’t be able to access the metafields that belong to it.