Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when updating metafields on a product #813

Closed
skillmatic-co opened this issue Dec 4, 2020 · 15 comments
Closed

Error when updating metafields on a product #813

skillmatic-co opened this issue Dec 4, 2020 · 15 comments

Comments

@skillmatic-co
Copy link

skillmatic-co commented Dec 4, 2020

When I try to do this:

shopify_product = ShopifyAPI::Product.find(123456)
shopify_product.metafields = [{
    key: "someKey",
    value: "new value,
    value_type: "string",
    namespace: "MyNamespace"
}]
shopify_product.save

I'm getting this error: :key=>["must be unique within this namespace on this resource"]

This namespace/key already exists on this product, I'm just trying to update its value to "new value", but this error is preventing it. How do I do this?

#419 could be related.

@skillmatic-co skillmatic-co changed the title How do you update metafields on a product? Error when updating metafields on a product Dec 4, 2020
@skillmatic-co
Copy link
Author

This method worked for me, but uses extra API calls. Is there a way to make my original request work?

metafield = ShopifyAPI::Metafield.find(:first, :params=>{:resource => "products", :resource_id => 123456, :namespace => "MyNamespace", :key => "someKey"})
metafield.value = "new value"
metafield.save

This isn't ideal, because in my full code (I only posted a snippet for clarity above), I'm updating other order properties such as the product title and product vendor, and it would be ideal to only have to hit the API at ShopifyAPI::Product.find(), instead of having to also hit ShopifyAPI::Metafield.find() for all of the metafields I need to update, which is actually 3 of them in my actual scenario.

So I'm having to go from this:

product = ShopifyAPI::Product.find()
product.save

to

product = ShopifyAPI::Product.find()
product.save

metafield1 = ShopifyAPI::Metafield.find()
metafield1.save

metafield2 = ShopifyAPI::Metafield.find()
metafield2.save

metafield3 = ShopifyAPI::Metafield.find()
metafield3.save

Unless I'm missing something.

@thecodepixi
Copy link
Contributor

Hi, @skillmatic-co! That sounds super frustrating. I'll take a look into this for you and see if I can find you a better solution.

@thecodepixi
Copy link
Contributor

Something I did think of, if you're willing to delve into GraphQL, you may be able to use [this productUpdate mutation] (https://shopify.dev/docs/admin-api/graphql/reference/products-and-collections/productupdate) to update multiple metafields in a single request. This mutation accepts both metafields and privateMetafields as a part of the mutation input fields.

@damienlethiec
Copy link

damienlethiec commented Dec 7, 2020

Same problem for me, I can confirm the issue. And I use more of less the same workaround than skillmatic

@mllemango
Copy link
Contributor

Unfortunately the REST API doesn't support the action of updating metafields through the Product resource, you're only able to create new ones. Let me know if there are any more questions! I'll be closing the issue for now since this isn't a shopify_api bug.

@damienlethiec
Copy link

Well, can it become a feature request? ;)

@mllemango
Copy link
Contributor

Pass this suggestion along to to Partner Support! :)

@simplenotezy
Copy link

Shopify API should really support this. It takes extreme overhead to update a single entity.

@Benzer1406
Copy link

Agree, this should be supported. Facing the same issue for customers.

@asalazarsgv
Copy link

asalazarsgv commented May 19, 2022

I think I could solve it!

First I got the id of the metafield like this:

endpoint = "metafields.json?metafield[owner_id]= (customer_id) &metafield[owner_resource]=customer"

Then with help of this reference: https://shopify.dev/api/admin-rest/2021-10/resources/metafield#put-metafields-metafield-id , I could update my customer's metafield.

endpoint = "customers/" + (customer_id) + "/metafields/" + (metafield_id) + ".json"
json = {"metafield":{"id":(metafield_id) ,"value": (metafield_value),"type":"single_line_text_field"}}
response = requests.put(url + endpoint,json=json)

I hope it helps!

@BernardoOficial
Copy link

BernardoOficial commented Jun 15, 2022

Hello guys. my name is Bernardo.

There is a way to update the metafields without so many problems. I hope my example helps you.

To update the metafield through the customer API it is necessary to pass the id of the metafield that will work.

Enter in:
{domain}/admin/customers/{customer_id}/metafields
So that all metafields created for the customer are listed. In this list, you can find out which is the id of the metafield that you need to update.

Then just run this code snippet that updates the customer data:

const body = {
	customer: {
		id,
		first_name,
		last_name,
		phone,
		metafields: [
			{ id: 20263173128307, namespace: "my_fields", type: "date", key: "bithday", value: bithday }
		],
	}
}

try {
	const response = await this.shopifyStore.put({
		path: `/admin/api/2022-04/customers/${id}.json`, 
		data: body, 
		type: DataType.JSON 
	});
	
	const customer = response.body;
	console.log(customer);
	
} catch (error) {
	throw new Error(error.message);
}

@jonaspohren
Copy link

You can use the Product API to update or create a metafield when you call PUT /products/PRODUCT_ID.json

{
  product: {
    metafields: [
      { id: 12345, value: "" }, // update a metafield
      { key: "", value: "", type: "single_line_text_field", namespace: "global" } // create a new metafield
    ]
  }
}

But after you save a product you don't get the metafields, so you need to call first /metafields.json?metafield[owner_id]=PRODUCT_ID&metafield[owner_resource]=product

@abhishekdubey-salesforce
Copy link

abhishekdubey-salesforce commented Dec 7, 2023

I'm using GraphQL productUpdate mutation, facing the same issue, not being able to update the value of metafields, it says
"Key must be unique within this namespace on this resource". Can't we do it in a single graphQL request while updating the other product fields ?

@bigware
Copy link

bigware commented Dec 13, 2023

Once there is a namespace/key combination for a product, you need to send the ID of the metafield along with the changes. Otherwise, you will get the "Key must be unique within this namespace on this resource" error. The best is to store the metafield ID locally when you first create the product.

@umutnacak
Copy link

If you are not updating other fields of the product then you can use metafieldsSet instead.
https://shopify.dev/docs/api/admin-graphql/2024-01/mutations/metafieldsset
Sets metafield values. Metafield values will be set regardless if they were previously created or not.

Manage metafields section of the documentation doesn't even mention this mutation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests