6

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] => https://www.domainzzz.com/wp-content/uploads/2019/05/MB-150x150.jpg
                    [1] => 150
                    [2] => 150
                    [3] => 1
                )

            [position] => 0
        )

    [1] => Array
        (
            [0] => https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2072-copy.png
            [position] => 1
        )

    [2] => Array
        (
            [0] => https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2071-copy.png
            [position] => 2
        )

)

And this is the full request:

$api_response = wp_remote_post( 'https://newdomain.com/wp-json/wc/v2/products', [
        '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 http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
        ]
    ] );
thelaw
  • 385
  • 5
  • 12

2 Answers2

0

The correct format of the image array should be

$images = array(
    array(
        'src' => 'https://www.domainzzz.com/wp-content/uploads/2019/05/MB-150x150.jpg',
        'position' => 0
    ),
    array(
        'src' => 'https://www.domainzzz.com/wp-content/uploads/2020/04/MG_2072-copy.png',
        'position' => 1
    )
)
Moshe Gross
  • 1,206
  • 1
  • 7
  • 14
0
$api_response = wp_remote_post('https://newdomain.com/wp-json/wc/v2/products', [
    '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' => [
            [
                'id' => 9
            ],
            [
                'id' => 14
            ]
        ],
        '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'
            ]
        ],
        'regular_price' => $product->get_price() // product price
        // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
    ]
]);
mujuonly
  • 11,370
  • 5
  • 45
  • 75