2

I'm having difficulty in updating an existing metafield with Shopify API. Each time I receive an error, advising me that the variant already exists... so it must be thinking I'm trying to create a new one (and not update).

I thought this may be an issue with 'put' and 'post' - so changed my method to put, however the error persists. I've hardwired in all my variables to make it easier to test.

I'm working with Cloudinary. I'm using https://github.com/sinechris/shopify-node-api with Express.js

app.post('/upload', function(req, res){
  // upload page... assume we have uploaded our image - but have hard-wired a local file in for now
  cloudinary.uploader.upload('/Users/Rob/Pictures/testimg.jpg', function(savedImg) { 
    var imageURL = savedImg.url;
    console.log(imageURL)
  },
  {
    public_id: "testimg"
  });


  // the saved image is returned - so we add it to our updateMetafieldData json object
      var updateMetafieldData = {
       "variant": {
          "id": '253818949',
          "metafields": [
            {
              "key": "variant_image_0",
              "value": 'testimg', // whatever the public id of our image is.
              "value_type": "string",
              "namespace": "variant_image"
            }
          ]
        }
      }
  // and post the result to shopify - then redirect to /getvariants
  Shopify.put('/admin/variants/253818949.json', updateMetafieldData, function(data){
    // res.redirect('/getvariants')
  });



});
1
  • I've sort of stepped around the issue by deleting the metafield... but would ideally like to do an update. Commented May 12, 2014 at 9:30

1 Answer 1

1

I actually created Shopify Node API and was just now happened upon this months later but thought I'd answer for anyone else coming along.

Take a look at the shopify API here: https://docs.shopify.com/api/metafield#update

You can update the metafield directly by performing a PUT request against the metafield resource instead of the variant like so:

/admin/metafields/#{id}.json

You would of course need to know the ID of the metafield first so that would require a call to the variant first or you could simply store the id in your local database for reference.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please answer this stackoverflow.com/questions/47432209/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.