WooCommerce Gallery Hover for Elementor Loop

Displays the first image of the gallery when hovering over the featured image in Elementor loops.

Create a php file and put it in your mu-plugins folder

				
					<?php
/**
 * Plugin Name: WooCommerce Gallery Hover for Elementor Loop
 * Description: Affiche la première image de la galerie produit au survol de l'image mise en avant dans les boucles Elementor.
 * Version: 1.0.0
 * Author: Wycan
 * Requires Plugins: woocommerce
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Vérifie que WooCommerce est actif avant d'exécuter le plugin.
 */
function wc_gallery_hover_init() {
    if (!class_exists('WooCommerce')) {
        return;
    }

    add_action('wp_footer', 'wc_gallery_hover_render');
}
add_action('plugins_loaded', 'wc_gallery_hover_init');

/**
 * Injecte le CSS + JS et les données de galerie dans le footer.
 */
function wc_gallery_hover_render() {
    $products = wc_get_products([
        'status' => 'publish',
        'limit'  => -1,
        'return' => 'ids',
    ]);

    $gallery_data = [];

    foreach ($products as $product_id) {
        $product = wc_get_product($product_id);
        if (!$product) {
            continue;
        }

        $gallery_ids = $product->get_gallery_image_ids();
        if (!empty($gallery_ids)) {
            $img_url = wp_get_attachment_image_url($gallery_ids[0], 'large');
            if ($img_url) {
                $gallery_data[$product_id] = $img_url;
            }
        }
    }

    if (empty($gallery_data)) {
        return;
    }
    ?>
    <style id="wc-gallery-hover-css">
        img.secondary-image-hover {
            position: absolute !important;
            top: 0 !important;
            left: 0 !important;
            width: 100% !important;
            height: 100% !important;
            object-fit: cover !important;
            opacity: 0 !important;
            transition: opacity 0.35s ease !important;
            pointer-events: none !important;
        }
        .e-loop-item:hover img.secondary-image-hover {
            opacity: 1 !important;
        }
    </style>
    <script id="wc-gallery-hover-js">
    document.addEventListener('DOMContentLoaded', function () {
        var galleryData = <?php echo wp_json_encode($gallery_data); ?>;

        document.querySelectorAll('.e-loop-item').forEach(function (item) {
            var match = item.className.match(/post-(\d+)/);
            if (!match) return;

            var postId = match[1];
            if (!galleryData[postId]) return;

            var imgWidget = item.querySelector('.elementor-widget-theme-post-featured-image');
            if (!imgWidget) return;

            var mainImg = imgWidget.querySelector('img');
            if (!mainImg) return;

            var container = mainImg.parentElement;
            container.style.position = 'relative';
            container.style.overflow = 'hidden';

            var secondary = document.createElement('img');
            secondary.src = galleryData[postId];
            secondary.classList.add('secondary-image-hover');
            secondary.alt = mainImg.alt || '';
            secondary.loading = 'lazy';
            container.appendChild(secondary);
        });
    });
    </script>
    <?php
}

				
			
Want to leave a comment ?

Leave a Reply

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

Want to see more code snippets?

Back to Top for Elementor

“Back to Top” button with progressive fill based on scroll. Automatically uses Elementor's main color.

WooCommerce Gallery Hover for Elementor Loop

Displays the first image in the gallery when hovering over the featured image in Elementor loops.

Free shipping method with WooCommerce coupon

Offer free shipping… but only for the shipping method you choose.

WooCommerce Product Buyers List

Added a tab in WooCommerce to display buyers of a specific product with date filter.

Additional Emails per Products for WooCommerce

Adds a custom tab in WooCommerce products to write an email specific to each product. This will be sent in a dedicated email.

Create a custom gallery with Elementor

With this code we gonna add a custom gallery to an elementor page
Let's talk ?

A question about WordPress?
A web project to be outsourced to a freelancer?
I am your man!