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')
  });



});
Rob
  • 1,576
  • 3
  • 22
  • 52
  • I've sort of stepped around the issue by deleting the metafield... but would ideally like to do an update. – Rob May 12 '14 at 09:30

1 Answers1

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.

ChrisC
  • 916
  • 1
  • 10
  • 24
  • Can you please answer this https://stackoverflow.com/questions/47432209/shopify-get-shop-domain-inside-a-app –  Nov 22 '17 at 14:43