1
votes

I have a problem with woocommerce rest api. I want to create a product with photos via rest api, but it works only without photos.

This snippet of code work:

{ "regular_price": "21.99", "name": "Premium Quality", "description": "Pellentesque habitant morbi tristique senectus et netus", "type": "simple" }

but this doesn't work:

{ "regular_price": "21.99", "images": "[{\"src\":\"https://static01.nyt.com/images/2017/03/17/world/europe/oakImage-1489777706390/oakImage-1489777706390-largeHorizontal375.jpg\",\"position\":0,\"id\":0}]", "name": "Premium Quality", "description": "Pellentesque habitant morbi tristique senectus et netus", "type": "simple" }

The JSON is validate. The response from the server is:

{ "code": "woocommerce_product_invalid_image_id", "message": "#0 is an invalid image ID.", "data": { "status": 400 } }

1

1 Answers

0
votes

The documentation claims:

Image ID (attachment ID). In write-mode used to attach pre-existing images.

According to that, you should send this request without the id parameter in your image object. This is because there is no pre-existing image with the id=0, I guess.

Example code taken from the docs:

{
  name: 'Premium Quality',
  type: 'simple',
  regular_price: '21.99',
  description: 'Pellentesque habitant morbi...',
  short_descriptienter code hereon: 'Pellentesque ...',
  categories: [
    {
      id: 9
    },
    {
      id: 14
    }
  ],
  images: [
    {
      src: 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',
      position: 0
    },
    {
      src: 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
      position: 1
    }
  ]
};