Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hi there! I am trying to change metafields data for any given product.
The request succeeds but data isn't updated.
I have attached a photo to illustrate.
Here's the text version of this code:
mutation { productUpdate(input: { id: "gid://shopify/Product/8441259336", metafields: [ { namespace: "global", key: "wish_count", value: "550", } ] }) { product { metafield(namespace: "global", key: "wish_count") { id namespace key value } } } }
Solved! Go to the solution
This is an accepted solution.
Hi Arkadi,
It looks like the issue you are running into here is that your mutation is missing the valueType parameter in the input. In this case, you will most likely want to set this to "INTEGER".
In the future, I recommend that you try running these mutations in the Shopify GraphiQL App and including the userErrors return field to get the most amount of feedback. For your mutation, including the userErrors would have given you the information to help fix the issue. I've included an example below.
mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { metafield(namespace: "global", key: "wish_count") { id namespace key value } } userErrors { field message } } }
Variables:
{ "input": { "id": "gid://shopify/Product/8441259336", "metafields": { "namespace": "global", "key": "wish_count", "value": "550", "valueType": "INTEGER" } } }
If you run into any other issues, please reach out!
To learn more visit the Shopify Help Center or the Community Blog.
This is an accepted solution.
Hi Arkadi,
It looks like the issue you are running into here is that your mutation is missing the valueType parameter in the input. In this case, you will most likely want to set this to "INTEGER".
In the future, I recommend that you try running these mutations in the Shopify GraphiQL App and including the userErrors return field to get the most amount of feedback. For your mutation, including the userErrors would have given you the information to help fix the issue. I've included an example below.
mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { metafield(namespace: "global", key: "wish_count") { id namespace key value } } userErrors { field message } } }
Variables:
{ "input": { "id": "gid://shopify/Product/8441259336", "metafields": { "namespace": "global", "key": "wish_count", "value": "550", "valueType": "INTEGER" } } }
If you run into any other issues, please reach out!
To learn more visit the Shopify Help Center or the Community Blog.
Thanks ntan for your quick answer
Tried it too, still does not work.
we get similar response even when adding the valyeType as you suggested
meaybe its a matter of permissions? the get request works well but we cannot update, create or delete
and this is the reply we get
Hi rchenx,
I realize now that what you're trying to do is update the value of an existing metafield on a product, which cannot actually be done with the mutation we are using here. Instead, we should be using the privateMetafieldUpsert mutation instead. Take a look at the tutorial I have linked here. That should give you the information you need to update the product.
I recognize that this is a bit confusing and not very intuitive, so I will speak to the team here about what we can do to make the documentation clearer on how to handle this use case. Please let me know if you run into any other issues.
To learn more visit the Shopify Help Center or the Community Blog.
Hello ntan,
We are interested in changing PUBLIC metafield and not private metafield becouse we need the shopify liquid code to utize those values
how that should be addressed?
Best
Ronen
I would also like to add that in this tutorial:
https://shopify.dev/tutorials/manage-metafields-with-graphql-admin-api
updating metafields only exists for private metafields.
How about the public ones too? we need to access those fields in shopify liquid... We can't use private metafields.
the POST of new public metafields works, but we need to UPDATE as well. When we try to do this with the public metafields this is thew error returned:
"Key must be unique within this namespace on this resource".
Hi folks,
My mistake for suggesting the privateMetafieldUpsert mutation. You are correct in that it is the wrong thing to be using here. Instead, we should be using the productUpdate mutation as you originally thought, except instead of passing in the key as the identifier, we should use the actual id of the metafield itself. I have included a sample of what the mutation should look like below:
mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { metafield(namespace: "global", key: "wish_count") { id namespace key value } } userErrors { field message } }
Variables:
{ "input": { "id": "gid://shopify/Product/8441259336", "metafields": { "id": "gid://shopify/Metafield/11975849934931", "value": "550", "valueType": "INTEGER" } } }
Please let me know if this works for you.
To learn more visit the Shopify Help Center or the Community Blog.
I get a "ProductUpdate access denied" when trying your method.
Is it possible to update public product metafield?
Access denied typically indicates that your API credentials don't have the access scope permissions to perform the operation. If you edit the app permissions in the Shopify web admin and reauth your app then it should work.
I got this error using the Shopify GraphiQL App from my shop page.
(At the end I don't mean to use it through an app but directly in a lambda function)
So if you go into the Shopify web admin for this shop and go into your logged-in user account. See what permissions are listed there. If you are using the Shopify GraphiQL app then the permissions flow from the logged-in user.
It's the main root account.
Mind providing a full screen shot of the GraphiQL app query that's failing?
I just ran this for a ProductVariantUpdate using the GraphiQL app in my test store. It worked fine. See below. I didn't have an exiting product with a defined metafield so it was against a variant child, but seemed to go okay. And you've verified that this particular Shopify account has the appropriate permissions? I know you said that it's the root account, but double-checking might not hurt!
Well, you were right! Thx for insisting. 🙂
I wasn't able to review full rights of the application (or at least they were misleading) but eventually I reinstalled it and it worked.
Side note: it seems to me that the example here: https://shopify.dev/tutorials/manage-metafields-with-graphql-admin-api#updating-the-owning-resource
Lack with the field
Otherwise you get a: "message": "Key must be unique within this namespace on this resource"
in the latest api version namespace and key fields are mandatory, so I continue to get
hey mate, I'm sending this mutation:
mutation {
productUpdate(input: {
id: "gid://shopify/Product/6653113303204",
title: "zinc test",
metafields: [
{
namespace: "global",
key: "title_tag",
value: "zinc test",
valueType: STRING
}
]
}) {
product {
id
metafields(first: 10, after: null) {
edges {
node {
key
namespace
value
}
}
}
}
userErrors {
field
message
}
}
}
and getting this response:
{
"data": {
"productUpdate": {
"product": {
"id": "gid:\/\/shopify\/Product\/6653113303204",
"metafields": {
"edges": []
}
},
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 22,
"actualQueryCost": 12,
"throttleStatus": {
"maximumAvailable": 1000.0,
"currentlyAvailable": 988,
"restoreRate": 50.0
}
}
}
}
I'm trying to create the metafield
In your query, metafield valueType is not in quotes. It should be "STRING" instead of STRING.
Could that help?
Hey @Iipa , that's incorrect. It has to be without quotes. The problem was that the gobal title tag can't have the same value as the product's title.