6

Is there a way (maybe via functions.php) to change the product-image in woocommerce shops (archive page) on hover with the first attached gallery image of the product? I cannot find how to target both. I guess it must be sth like this:

add_action( 'woocommerce_shop_loop_item', 'change_image_on_hover', 10 );
function change_image_on_hover() {
     $product->get_gallery_image_ids();
     print 'woocommerce_gallery_image';
}

enter image description here

Krystian
  • 887
  • 5
  • 21
  • 52

7 Answers7

14

What I think you'll want to do, assuming your installation is a somewhat standard WooC installation, is utilize the loop item action hook to add the desired on-hover image.

add_action( 'woocommerce_before_shop_loop_item_title', 'add_on_hover_shop_loop_image' ) ; 

function add_on_hover_shop_loop_image() {

    $image_id = wc_get_product()->get_gallery_image_ids()[1] ; 

    if ( $image_id ) {

        echo wp_get_attachment_image( $image_id ) ;

    } else {  //assuming not all products have galleries set

        echo wp_get_attachment_image( wc_get_product()->get_image_id() ) ; 

    }

}

Preliminary CSS:

/* CUSTOM ON-HOVER IMAGE */
.woocommerce ul.products li.product a img { 
    /* FORMAT ALL IMAGES TO FILL EQUIVALENT SPACE,
    to remove jitter on replacement */
    height: 150px;
    width: 150px;
    object-fit: cover;
    padding: 0;
    margin: 0 auto;
}
.woocommerce ul.products li.product a img:nth-of-type(2) {
    display: none;
}
.woocommerce ul.products li.product a:hover img:nth-of-type(2) {
    display: block
}
.woocommerce ul.products li.product a:hover img:nth-of-type(1) {
    display: none;
}

The above will get what you want for one type of shop archive display, to achieve a simple replacement, but there will be numerous particulars that you may need or want to customize for your site. 150x150px may not be the right size for your theme, for example. Or you may decide you need to replace the default image completely with a different set, and that to get a particular transition effect, or to get consistency with other effects in use on your site, you'll need a different approach to CSS and possibly to javascript.

CK MacLeod
  • 932
  • 6
  • 10
  • 1
    Awesome, this is working, thank you very much. Unfortunatly it prints the 150x150px version of the gallery image. Do you know to get the better quality image with 600x600px? CSS isnt the solution. – Krystian Jun 05 '20 at 07:28
  • 2
    Sure - wp_get_attachment_image() defaults to 'thumbnail'. If your theme has 600x600 alreqady registered as an image size ( 'medium'?, 'large'?), then just add it as second parameter. or try 'array( '600', '600' ) (and adjust CSS appropriately) - More with examples at https://developer.wordpress.org/reference/functions/wp_get_attachment_image/ – CK MacLeod Jun 05 '20 at 07:35
  • 1
    ("medium" and "large" would more likely be set in installation Settings/Media) – CK MacLeod Jun 05 '20 at 07:42
  • 3
    To get the first gallery image I think it should be: get_gallery_image_ids()[0] – Alexander Feb 08 '21 at 12:07
  • 2
    The [0] image is, I think, the featured image... but it's been a while since I answered this.I'll double-check later if you haven't done so aready. – CK MacLeod Feb 08 '21 at 22:57
  • Can u do a Update of the Code for the current Version? The current Code pulls a low-quality preview image from the first gallery picture. i haven't figured it out yet, if i find out i let you know here. – Risk Apr 14 '21 at 14:33
  • 1
    See discussion above for hints. – CK MacLeod Apr 14 '21 at 19:38
1

You can use the plugin if you have not removed the default action and filter of WooCommerce.

Here is the plugin link

Akshay Shah
  • 3,391
  • 2
  • 20
  • 33
  • thank you very much, in general this plugin is working, but if you keep your mouse over the picture it flips back and forwards in a endless loop instead of flipping back only if you move your mouse away. – Krystian Jun 01 '20 at 08:42
0

CSS can detect when the user is hovering over an image. Is it possible for you to load both images, and then just turn one off and one on when hover is detected?

The code for that would look like this:

HTML:

<a id="home">
<img class="image_on" src="images/productImage.png"" />
<img class="image_off" src="images/galleryImage.png" />
</a>

CSS:

.image_off, #home:hover .image_on{
     display:none
}
.image_on, #home:hover .image_off{
     display:block
}

If this doesn't work, check out this thread for other solution using js/jquery

willturner
  • 104
  • 6
0

Is this what you are looking for?

WooCommerce Product Image Flipper

It seems that it has not been tested with the latest 3 major Wordpress releases though. You can still try it and see.

This thread also seems to explain what you need.

Bilaal Abdel Hassan
  • 1,181
  • 2
  • 12
  • 20
0

Use this code is working perfectly | Woocomerce Latest Version Switch product image on hover on WooCommerce archive page

add_action( 'woocommerce_before_shop_loop_item_title', 'add_on_hover_shop_loop_image' ) ; 

function add_on_hover_shop_loop_image() {

    $image_id = wc_get_product()->get_gallery_image_ids()[0] ; 

    if ( $image_id ) {

        echo wp_get_attachment_image( $image_id ) ;

    } else {  //assuming not all products have galleries set

        echo wp_get_attachment_image( wc_get_product()->get_image_id() ) ; 

    }

}
Ashar Zafar
  • 382
  • 2
  • 11
0

Use this you will get the full size image of the product

add_action( 'woocommerce_before_shop_loop_item_title', 'add_on_hover_shop_loop_image' ) ; 

function add_on_hover_shop_loop_image() {

$image_id = wc_get_product()->get_gallery_image_ids()[1] ; 
$img_size = wc_get_image_size( $image_id, 'full' );

if ( $image_id ) {

    echo wp_get_attachment_image( $image_id, 'full' ) ;

} else {  //assuming not all products have galleries set

    echo wp_get_attachment_image( wc_get_product()->get_image_id() ) ; 

}

}
Pedram
  • 15,766
  • 10
  • 44
  • 73
0

If you want to use this on product-content.php use global product instead of wc_get_product

global $product;
$attachment_ids = $product->get_gallery_image_ids();

Then:

foreach( $attachment_ids as $attachment_id ) {
     wp_get_attachment_image( $attachment_id )
}
Pedram
  • 15,766
  • 10
  • 44
  • 73