Sync Product Images to WP
Hi,
I am wondering how to sync product images to WP as the WOO API does not accept remote URL images.
Do you have any idea if it is possible to do that ?
Many thanks in advance.
BR
David
Here is the content I am sending : array:15 [▼ "name" => "in" "description" => "Error ratione nihil deleniti. Vel ut quibusdam ipsa ea. Qui rerum et voluptas sed sint. Neque inventore voluptatem et accusantium amet corporis sunt molestiae." "short_description" => "Error ratione nihil deleniti. Vel ut quibusdam ipsa ea. Qui rerum et voluptas sed sint. Neque inventore voluptatem et accusantium amet corporis sunt molestiae." "type" => "simple" "regular_price" => "875" "sale_price" => null "date_on_sale_from" => "2020-05-01T00:00:00" "date_on_sale_to" => "2020-05-01T23:59:59" "on_sale" => false "categories" => array:1 [▼ 0 => array:1 [▼ "id" => "27" ] ] "images" => array:2 [▼ 0 => array:1 [▼ "src" => "http://laravel6.test/media/b53b3a3d6ab90ce0268229151c9bde11/gruyere_1.jpg" ] 1 => array:1 [▼ "src" => "http://laravel6.test/media/9f61408e3afb633e50cdf1b20de6f466/gruyere_aop.jpg" ] ] "manage_stock" => true "stock_quantity" => 148.0 "stock_status" => "instock" "sku" => "3761524624710" ]
Note that the images are public accessible and I got the following message :
Automattic\WooCommerce\HttpClient\HttpClientException Error: Erreur d’obtention de l’image distante http://laravel6.test/media/b53b3a3d6ab90ce0268229151c9bde11/gruyere_1.jpg. Erreur : L’URL fournie n’est pas valide. [woocommerce_product_image_upload_error]
Hello @invaders-xx
Thanks for opening the issue. I checked your image url it's not working. Can you double check?
Best regards, MD Abu Ahsan Basir
Hello @maab16 ,
It's on my local computer ;) Sorry when I mentioned "public", I meant public vs needs auth
@invaders-xx
Can you try live link? I checked it works for me.
Thanks, Md Abu Ahsan Basir
Many thanks for your feedback. I will check and come back to you asap
Do you mean that it does not work on local host ? Is this a WP constraint ?
@invaders-xx
I think there is no problem with WP or localhost. Maybe image link or upload related issue. Here is an example that I tested and it works for me.
$data = [
'name' => 'Image Tester',
'type' => 'simple',
'regular_price' => '2.00',
'description' => 'Image product description.',
'short_description' => 'Image product short description.',
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
],
[
'src' => 'https://images.pexels.com/photos/3819572/pexels-photo-3819572.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260'
]
]
];
$product = Product::create($data);
Hope it works for you. If you have other problem you can create new issues.
Thanks
@maab16
I tried to use the exact same code as yours and I got this message :
Exception Error: #0 est un ID d’image non valide. [woocommerce_product_invalid_image_id]
I am using WOO API 4.0.1:
Thanks
Dear @maab16 ,
I made your example working fine.
I think it's a problem with local URL/host. I found a plugin which solved my problem : WP-Allowed-Hosts. I just add my laravel host in the list
@invaders-xx
Maybe but sounds good. If you have any other problem then make a new issue.
Thanks
hey,@maab16
I come cross this problem.
For the "simple" type product, it's ok For the "variable" type products, same code, can't work, so strange.
Regards Hqin2013
Hello @hqin2013
I think you can try @invaders-xx solution. Allow Laravel host in your WP.
Thanks
Hey, @maab16
Maybe I didn't write the problem clearly.
For "variable" type product, i can create product gallery (so Allow host is not problem).
But can't create thumbnail for variations , this is the problem.
$variation_data = [ 'sku' => $sku, 'regular_price' => $regular_price, 'manage_stock' => true, 'stock_status' => $stock_status, 'stock_quantity' => $quantity->quantity, 'weight' => $weight, 'dimensions' => [ 'length' => $length, 'width' => $width, 'height' => $height, ], 'attributes' => [ [ 'name' => 'Color', 'option' => $color, ], ], 'images' => $images ];
$images is array:
Array ( [0] => Array ( [src] => URL ) [1] => Array ( [src] => URL ) )
@hqin2013
I think there is no option to set thumbnails via woocommerce rest api. Please read the official docs https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
Thanks
sadly, I found some application can do this.
thank you for help @maab16
@hqin2013
First of all congratulation. But I don't understand what do you mean not working? There is no option to set thumbnails directly using rest API but it sets thumbnails automatically when pass images.
The first image is set for product image and also first thumbnail image. Next images set for thumbnails. I created a variable products now and display all images as thumbnails that I passed when create the variable product. here I attached two attachment one for product edit page and another for single product page where it display all thumbnails.

If you mean other issue please send screenshot or details.
Thanks
Hey,@maab16
variable product has 2 colors: one is blue, another is back
want to set thumbnail for blue and black
Is it possible?
Regards Qin
@hqin2013
Yes, you can do it. Please read documentation properly. I attached two links here
https://codexshaper.github.io/docs/laravel-woocommerce/ https://woocommerce.github.io/woocommerce-rest-api-docs/#product-variation-image-properties
Here is the example how to create a variation product. Here I used 4 images
$data = [
'name' => 'Ship Your Idea',
'type' => 'variable',
'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
'categories' => [
[
'id' => 9
],
[
'id' => 14
]
],
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg'
],
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg'
],
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg'
],
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg'
]
],
'attributes' => [
[
'id' => 6,
'position' => 0,
'visible' => false,
'variation' => true,
'options' => [
'Black',
'Green'
]
],
[
'name' => 'Size',
'position' => 0,
'visible' => true,
'variation' => true,
'options' => [
'S',
'M'
]
]
],
'default_attributes' => [
[
'id' => 6,
'option' => 'Black'
],
[
'name' => 'Size',
'option' => 'S'
]
]
];
$product = Product::create($data);
In my case product_id is 230 and I've 4 images for my variation product 226, 227, 228, 229. This images I created when create variation product.
Now I'll create a variation with image. You need to pass image id when create new variation. In this example I passed first image_id (226).
$data = [
'regular_price' => '9.00',
'image' => [
'id' => 226
],
'attributes' => [
[
'name' => 'Size',
'option' => 'M'
]
]
];
$variation = Variation::create(230, $data);
Check this hope your issue will solve. I attached a screenshot.
Thanks

Hey, After several times try, finally figure it out. (in fact, I made a mistake,image )
Thank you,you're always a kind man. @maab16
@hqin2013
Sounds good. You are always welcome.
Hello, I know this problem is a few years ago, but I want to share a solution I found to this problem of uploading images of products from a local server. In case someone serves you simply follow this solution:
Just drop this in functions.php but remove it after you import into a live or production server as it probably opens your website up to some malicious activity:
function github16702_allow_unsafe_urls($args, $url) {
$args['reject_unsafe_urls'] = false;
return $args;
}
add_filter('http_request_args', 'github16702_allow_unsafe_urls', 20, 2 );
If you are using php artisan serve or similar, it worked for me to change the permissions of the directory where the images are saved to everyone can read and instead of passing the address of the laravel server, in my case http://127.0.0.1:8000/storage/tmp/image.png I passed the address of port 80, ie, http://127.0.0.1/path/to/image.png
Source: https://github.com/woocommerce/woocommerce/issues/16702#issuecomment-428900680