Naveen
Naveen

Reputation: 683

Unable to post a product with images using WooCommerce NodeJS API

I have been trying to insert a product using nodejs woocommerce api (v3) with the code below. However, the code only works when I remove the marked section. Otherwise I get the error code 400.

var WooCommerce = require('woocommerce');

var wooCommerce = new WooCommerce({
  url: 'http://mysite',
  consumerKey: 'ck_XXXXXX',
  secret: 'cs_XXXXXX'
});

var data = {
  product: {
    title: 'Product',
    regular_price: '21.99',
    description: 'This is an awesome product', 

    //################ WORKS WHEN THIS SECTION IS REMOVED ##############

    images: [
      {
        src: 'http://www.gstatic.com/webp/gallery/4.jpg',
        position: 0
      }
    ]

    //###################################################################

  }
};

wooCommerce.post('/products', data, function(err, data, res) {
  console.log(res);
});

How can I add images to the product along with this API call? Thanks in advance!

Upvotes: 0

Views: 788

Answers (2)

Naveen
Naveen

Reputation: 683

Quoting the keys fixed the problem!

var WooCommerce = require('woocommerce');

var wooCommerce = new WooCommerce({
  url: 'http://mysite',
  consumerKey: 'ck_XXXXXX',
  secret: 'cs_XXXXXX'
});

var data = {
  "product": {
    "title": 'Product',
    "regular_price": '21.99',
    "description": 'This is an awesome product', 

    //##################################################################

    "images": [
      {
        "src": 'http://www.gstatic.com/webp/gallery/4.jpg',
        "position": 0
      }
    ]

    //###################################################################

  }
};

wooCommerce.post('/products', data, function(err, data, res) {
  console.log(res);
});

Upvotes: 0

Dylan Wright
Dylan Wright

Reputation: 1202

You could do something like:

var value = Object.assign(product, images)
data = value;

I think as along as your arrays are one to one this could work. Or you could simply just create your own object and plugin the values as you go.

Upvotes: 1

Related Questions