You could upload 10 images, one for each color of your variable product… or you could be slightly smarter and use 1 image only, and then when the user selects a color trigger a background color change!
Easier to code than to explain, so let’s take a look at the screenshot below (image must be a PNG with transparent background) and the PHP snippet. This could be a time-saver, enjoy!
PHP Snippet: Switch Featured Image Background Color On Color Variation Select @ Single Product Page
Please note, your product attribute must be “Color” – otherwise you need to change the ‘#pa_color’ selector accordingly in the snippet below.
/**
* @snippet Switch Background @ WooCommerce Variable Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_after_variations_form', 'bbloomer_switch_image_background_variable_colors' );
function bbloomer_switch_image_background_variable_colors() {
wc_enqueue_js( "
$('input.variation_id').change(function(){
if('' != $('input.variation_id').val()) {
if($('#pa_color').val() =='red'){
$('.woocommerce-product-gallery__image').css('background-color', 'red');
} else if($('#pa_color').val() =='yellow'){
$('.woocommerce-product-gallery__image').css('background-color', 'yellow');
}
} else {
$('.woocommerce-product-gallery__image').css('background-color', 'transparent');
}
});
");
}
It, actually works, but you need to change the class of the image gallery
Thank you! I just revised the snippet
Is it possible to do the same with the Variation gallery and change the image based on selected color while having 2 attributes with 2 variations like color + size. But use something similar to change images based on selected color and don’t wait to choose size?
Hi Zimmer, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo,
Code works great. Is there a way to make it autonomous so that we don’t have to code the available colors names and hex numbers? If you just get the selected color and adjust the background color acording to the color that is set on the selected attribute?
Hi Chris, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
How to change the background color for the zoomImg and pswp_img classes?
The color does not keep with zoom and new tab open.
Can’t help here sorry
Wow Brilliant! I never had to use variants so far but definitely will visit back when it comes. As always you are the best!
Thanks Peter!
Hi Rodolfo,
I couldn’t get it working,
I added a variable product with a color option. I can see the JS function is loading in my browser, but I am guessing the class or ID I am trying to reference is wrong. Something that I really havent got my head around fully yet.
It could be you have a custom theme and something may need to be changed
I got it sorted,
I I am just using storefront with a pretty vanilla child theme.
This is what I did:
Thanks for your reply though.
Cheers,
Aaron.
Nice!