0

I am trying to add a product with an image via the WooCommerce REST API but getting this error:

Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: #0 is an invalid image ID.

Despite many other threads similar, no one seems to have an actual answer to why this happens despite the same code in the example of the docs. The image I am trying to use is not already in the media library.

The code:

$woocommerce = new Client(
  'https://www.example.co.uk',
  '123456789',
  '123456789',
  [
      'wp_api' => true,
      'version' => 'wc/v3',
      'sslverify' => false,
  ]
);

$data = array();

foreach ( $records->getRecords() as $record ) {

  $data = [
    'name' => $record['TITLE'],
    'description' => $record['DESCRIPTION'],
    'regular_price' => $record['PRICE'],
    'quantity' => $record['QUANTITY'],
    'images' => [
      'src' => 'https://via.placeholder.com/350x150.png',
    ],
  ];

  $woocommerce->post( 'products', $data );
}

I see nothing wrong. It’s setup in the same way as the example too: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product

0

Ok my mistake, you can see my images array was not quite right:

This is correct:

'images' => [
  [
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
  ],
  [
      'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
  ]
],

This was mine:

'images' => [
  'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
],

Thought it would be best to leave this question/answer as over the years there are many questions about this online with the same error.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.