I want to store privateMetafields with Shopify resources and access them in liquid files!
I don’t want to expose data to other apps or merchants so storing in metafields is not an option. But I can’t seem to access privateMetafields from liquid templates,I read in the docs that
privateMetafields can’t be accessed from liquid files
meta fields can be read/edited by accessed by merchants or other apps
Now, using meta fields to store dynamic data for Shopify resources is a security risk and privateMetafields can’t be accessed from liquid so it kinda defeats the purpose! I would need to have an API call or something to get data into templates.
I am running into this exact same issue. I want to use the new Theme App Extensions so that customers can add my app blocks within the theme customizer. I can pass the data to the theme extension’s Liquid templates through public JSON metafields, however that is going to confuse storeowners when they see it. I would much rather store it in private metafields so they don’t have to see it, and so they can’t mess things up by trying to edit it.
I was happy with ScriptTag implementation. Online Store 2.0 and Theme Extensions isn’t featured enough for my business logic, or I’m missing something for weeks.
As of GraphQL Admin API version 2022-04, a new owner type ApiPermission is now available for metafields. A metafield with this permission type will only be readable and writable by the app that owns the metafield.
The app metafields are actually on app installations (as one app can have many installations based on merchants). Now these are somewhat safer than putting data on shop owned public metafields.
These are accessilbe in the liquid file with the app object. {{ app.metafelds.namespace.key}}
Step1: Get App installation id
query {
currentAppInstallation {
id
}
}
Step 2: Use that id as owner id to save metafield(s)
mutation ($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
}
userErrors {
code
field
message
}
}
}
Make sure you set the orwnerId to the **appInstallationId** (which is unique for every installation of the app). When you use the appInstallationId for the ownerId field of the metafield, the meta field is created for that specific installation and will be removed automatically when the app is uninstalled.
You can make sure if the meta field is created or not using the following query:
@Anonymous Great if you could get the meta field using the query above, that makes sure that we indeed created the meta field with this specific app installation. Now just access those metafields in the theme app extension with {{ app.metafields.namespace.key }} in the liquid file (You’ll need to replace your namespace and key here).
If you still don’t see that meta field in the theme app extension, one caveat I can think of is you are trying to access a JSON field, without the “.value” method. If this is the case, the best would be just set a metafeild to a string value and make sure you can access that in the theme app extension using the {{app.metafields.namespace.key}}
When you are sure you can access the string metafield(s) then you can experiment with JSON fields.
Could you please confirm if you were able to access the App Owned Metafields within the Theme Liquid Files as well?, They are coming blank for me. It looks like they can be accessed via App Theme Extensions only?
@rohan_5894 yes you can access that meta filed into the theme liquid file. if you facing any Errors please share what you do still now write all the steps by step so I can guide you the right way.
my app meta filed value access on theme liquid file and also app extension code too.