Woocommerce version: 4.1.0
I am trying to create a product with categories and images via the API but I get this error :
{"code":"woocommerce_product_invalid_image_id","message":"#0 is an invalid image ID.","data":{"status":400}}
I have checked some answers but I use image extension in the url, I use only the url not the id and I am not using W3 Cache plugin. Here is the images array of the request:
Array
(
[0] => Array
(
[0] => Array
(
[0] => .jpg
[1] => 150
[2] => 150
[3] => 1
)
[position] => 0
)
[1] => Array
(
[0] => .png
[position] => 1
)
[2] => Array
(
[0] => .png
[position] => 2
)
)
And this is the full request:
$api_response = wp_remote_post( '', [
'headers' => [
'Authorization' => 'Basic ' . base64_encode( 'ck_306dd02a04f81cc33df:cs_4b3c7be0a0' )
],
'body' => [
'name' => $product->get_name(), // product title
'slug' => $product->get_slug(),
'sku' => $product->get_sku(),
'description' => $product->get_description(),
'status' => get_post_status($post_id), // product status, default: publish
'categories' => $cat_ids,
'images' => $images,
'regular_price' => $product->get_price() // product price
// more params /?shell#product-properties
]
] );
EDIT Here is the code that produces the categories and images arrays:
$product = wc_get_product($post_id);
//Get the categories of the product
$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
$cat_ids = [];
$images =[];
foreach( $product_cats_ids as $pci){
array_push($cat_ids, ['id' => $pci]);
}
//IMAGES
//Get the featured image
$featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post_id));
//and push it in the images array with position = 0 (featured)
array_push($images, [$featured_image_url, 'position' => 0]);
//Get All the images
$attachmentIds = $product->get_gallery_image_ids();
$pos = 1;
foreach( $attachmentIds as $attachmentId )
{
array_push($images, [wp_get_attachment_url( $attachmentId ), 'position' => $pos]);
$pos++;
}