How to get the WooCommercer product variation image

I am building a WooCommerce theme but I’m having trouble getting the featured image to change when the user selects a product variation.

I have set different featured images for each variation on the backend, and this is the function I’m using to call the product’s featured image.

What should I add to this function in order to grab the variation’s featured image instead of the overall product featured image?

function show_featured_image() {
  global $product;
  echo 'div class="product_fetured_image"';
    echo 'div class="feautred_image"img src="' . get_the_post_thumbnail_url() . '"/div';
  echo '/div';
}

Topic woocommerce-offtopic Wordpress

Category Web


here is product set in display in cart in cart.php

$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

if ( ! $_product->is_visible() ) {
            echo $thumbnail;
        } else {
                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($_product->id), 'thumbnail' );
                        $cartimgsrc=UNAVAILABLEIMG;
                    if($thumb[0]){
                        $cartimgsrc=$thumb[0];

                   $product = wc_get_product($cart_item['product_id']);
                   if ( $product && $product->is_type( 'variable' ) ) {


                            $product = new WC_Product_Variable( $product_id );
                            $variations = $product->get_available_variations();
                            foreach ( $variations as $variation ) {
                                if($variation['variation_id'] == $cart_item['variation_id'] ){
                                    $cartimgsrc=$variation['image']['thumb_src'];
                                }   
                            }
                         }

                    }

                    echo "<a href='".$_product->get_permalink( $cart_item )."'> <img class='cartthumbimage' height='180px' width='180px' src='".$cartimgsrc."' /></a>";
                    //printf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) ), $thumbnail );
        }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.