fbpx
Want To Add Extra Images Per Product Variation For Free? Download Now

Adding images to variation gallery using REST API

Stefan Zahnd

Hi

I’m looking for a way to add images to the additional variations gallery using the REST API of WooCommerce. I already built an importer that imports all the required images into the media library using the WP API. Therefore, I only need to add the media ID to the list of additional images of the variation.

I found this ticket in your support database: https://getwooplugins.com/ticket/23572/. As it’s already 4 years old, I wonder if there is now a more accessible option available.

Thank you very much and best regards,

Stefan

Stefan Zahnd

Hi Hakik

Thank you for your reply. Ok, can someone maybe elaborate the “process” a bit more? From the guidance in the referenced ticket, I’m not truly sure what to do.

Thank you very much and best regards,

Stefan

Hakik Zaman

Hi Stefan,

Please check the official documentation to create a product using API: https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product

You can read this article to get an idea to insert/update products using API:

https://rudrastyh.com/woocommerce/rest-api-create-update-remove-products.html

Then follow this reply:

https://getwooplugins.com/ticket/23572/#comment-62853

In that filter, you are getting the $request parameter to push your own request. You may push your gallery image IDs using the additional request and update the post meta key woo_variation_gallery_images with those IDs.

Here is an example:
add_filter( 'woocommerce_rest_pre_insert_product_variation_object', function( $variation, $request, $creating ){
if ( is_array( $request['variation_images'] ) ) {
update_post_meta( $request['id'], 'woo_variation_gallery_images', $request['variation_images'] );

}
return apply_filters( "custom_woocommerce_rest_pre_insert_product_variation_object", $variation, $request, $creating );
}, 10, 3 );
I hope this will help. If you have more questions after the above snippets, I recommend you contact a Woo Developer or study API as much as you can.

Thank You

Stefan Zahnd

Hi Hakik

I just quickly wanted to thank you for your last message and let you know that I could add the variation images using the API.

Best regards, Stefan