Have your say in Community Polls: What was/is your greatest motivation to start your own business?

How to access metafield in namespace with reserved prefix in Liquid?

Solved

How to access metafield in namespace with reserved prefix in Liquid?

geiszla
Shopify Partner
5 0 5

I've been trying to figure out how to access a metafield of metaobject reference list type that has a namespace with reserved prefix in a theme app extension in Liquid. The metafield has the namespace `$app:product_options` and key `product_options` with type `list.metaobject_reference`.

 

This is my code in Liquid:

 

 

{% for option in product['$app:product_options'].product_options.value %}
  {{ option | json }}
{% endfor %}

 

 

This does not print anything.

 

I've confirmed that the metafield and the referenced metaobjects are on the product I'm looking at in the local development instance of the storefront, and the code app works (e.g., I can print the ID of the product).

 

I've tried:

  • Setting the `storefront` access of the metaobjects to `PUBLIC_READ` and the metafields' `admin` access to `PUBLIC_READ` as well.
  • Using just the key without the namespace on the `product.metafields` object.

Accessing the metaobjects directly from `shop.metaobjects` works by using the fully expanded name of the namespace (i.e. using `shop.metaobjects['app--12345678912--product_option']['app--12345678912--product-option-abcde123']`), however, this does not work for metafields (i.e., `

product.metafields['app--12345678912--product_options']['product_options']` does not print anything`).

 

Is there a way to access a metafield with reserved-prefixed namespace in Liquid? And if so, how and with what access control options?

 

Thank for your help.

Accepted Solution (1)

prakhar96
Shopify Partner
2 1 2

This is an accepted solution.

You can now use access control on Metafield definitions.

You can make the metafield's storefront access as "PUBLIC_READ". You can then access it in theme app extensions by expanding the namespace to - `app--<app_id>`

 

However, this is only available in the unstable version of Graphql API. 

 

https://shopify.dev/docs/api/admin-graphql/unstable/objects/MetafieldAccess

View solution in original post

Replies 4 (4)

minsko
Shopify Partner
28 3 6

I have been able to use app-namespaced metafields, with MERCHANT_READ access, via liquid using all of the forms:

{{ object.metafields.app--xxxxxxx--namespace.key }}
{{ object.metafields['app--xxxxxxx--namespace'].key }}
{{ object.metafields['app--xxxxxxx--namespace']['key'] }}


However, values may not be visible for 90 minutes after setting:  https://community.shopify.com/c/shopify-design/outdated-server-cached-metafield-values-shown-on-orde...

geiszla
Shopify Partner
5 0 5

Hmm, interesting... I can't make it work like this. Maybe it has sthg to do with the fact that I use metaobject reference list as the metafield type? I can't really think of anything else, since I already tried `MERCHANT_READ` and `PUBLIC_READ` on the `admin` access of the metafield and the metaobject `storefront` access is already `PUBLIC_READ`.

 

When I change the namespace not to include a reserved prefix, then I can access it normally. It could be cache related, but for some reason non-reserved namepsaces don't have the same problem.

prakhar96
Shopify Partner
2 1 2

This is an accepted solution.

You can now use access control on Metafield definitions.

You can make the metafield's storefront access as "PUBLIC_READ". You can then access it in theme app extensions by expanding the namespace to - `app--<app_id>`

 

However, this is only available in the unstable version of Graphql API. 

 

https://shopify.dev/docs/api/admin-graphql/unstable/objects/MetafieldAccess

andrewjaanuu
Shopify Partner
1 0 1

That API is stable now.