Get variation image from term slug

I am developing new features into the Woocommerce Swatches plugin: https://woocommerce.com/products/variation-swatches-and-photos/

What I want is to use the variation images for the swatch images so I do not need to upload twice.

I have hooked into the woocommerce_swatches_get_swatch_image filter and I use the code below to get all the variation images.

What I need is to only get the image for one of the variations where the slug is the right one. Fx. the image for variation with color blue should be the blue option.

function change_image_varition_source( $src, $value_slug, $tax_slug ) {
$product = new WC_Product_Variable( get_the_ID() );
$variations = $product->get_available_variations();

foreach ( $variations as $variation ) {

$src = $variation[‘image’][‘thumb_src’];
}

return $src ;
}
add_filter( ‘woocommerce_swatches_get_swatch_image’, ‘change_image_varition_source’, 10, 3 );

Read more here:: Get variation image from term slug

Leave a Reply

Your email address will not be published. Required fields are marked *