For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
I create a cart and checkout validation function extention,like the example demo:https://shopify.dev/docs/apps/checkout/validation/server-side
But i also need to use the input queries variants,so i write the input.graphql like this:
query Input($selectedCollectionIds: [ID!]!) {
cart {
buyerIdentity {
customer {
numberOfOrders
}
}
lines{
quantity
merchandise {
__typename
... on ProductVariant {
product{
id
handle
isInCollection: inAnyCollection(ids: $selectedCollectionIds)
}
}
}
}
}
}
And I know the next step is to set the metafields value like this:
mutation SetMetafield {
metafieldsSet(metafields: [
{
namespace: "$app:my-namespace",
key: "my-key",
ownerId: "OWNER_ID",
type: "json",
value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/1\",\"gid://shopify/Collection/2\",\"gid://shopify/Collection/3\"]}"
}
])
{
metafields {
id
}
}
}
But I don't know which owner to set the value of metafields to?
Hi could you clarify what exactly you are trying to build?
You can't set a metafield from within your Function code. Are you trying to set the metafield there? Or is this happening in your app server code?
To learn more visit the Shopify Help Center or the Community Blog.
Yes, I know that setting metafields must be in the server code. What I mean is, when I set metafields in the background, which ownerId: "OWNER_ID" to set?
You will use the ID of the resource that you want to store the metafield on.
If you are able to confirm the functionality you are looking to build I can give a better suggestion for what resource you should be setting the metafield on. If this will be a setting that will be read by your app you could store it on the Shop resource. So you will use the Shop ID as the owner ID.
To learn more visit the Shopify Help Center or the Community Blog.
I want to use the variants value of metafields in input.graphql of the cart and checkout validation extension, as I mentioned earlier: the $selectedCollectionIds variable. May I ask which resource Id do I want to set to?
A common owner for the metafield would be the resource that owns the function. In this case the owner would be the Shop. So you would set the owner ID of the metafield to the Shop ID.
To learn more visit the Shopify Help Center or the Community Blog.
I have set metafields to owner shop, but I still cannot obtain input variables.
my code:
metaFields: [{
namespace: "$app:my-namespace",
key: "test",
ownerId: "gid://shopify/Shop/1", //
type: "json",
value: JSON.stringify({
selectedCollectionIds: ["gid://shopify/Collection/1", "gid://shopify/Collection/2"]
}),
}]
And I confirm that metafields have been successfully set to the shopId which .
Also I have completed the configuration as follows in the shopify.extension.toml file:
[input.variables]
namespace = "$app:my-namespace"
key = "test"
It seems like everything is ready, but the value of the variable $selectedCollectionIds is still not available in input.graphql.
Did I miss anything?
Please help me take a look,It's not working when i set the metafields to shopid
If you want to use an app metafield, the owner id is the GID of the app, which you can find with a currentAppInstallation query (https://shopify.dev/docs/api/admin-graphql/2023-10/objects/appinstallation#queries)