0

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>';
}
2
  • there is already included this feature in the current WooCommerce release. You can set an overall image for the product and as “featured image”, AND you can set at least one image for every product variation. In the list of product variations click the little plus sign in the image placeholder, upload an image and “insert into article”. Repeat for every variation that should have a distinct image. On the product page the image will automatically change when you select a variation. good luck!
    – Aishan
    May 15 '17 at 10:50
  • Thanks @Aishan. I have set different images for every variation. However these don't show when a variation is selected as you can see here montenegrostudio.com/bium/producto/lucky-3s so I'm guessing there must be something wrong witht he function I built to call the images May 15 '17 at 13:15
0

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 );
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.