TransWikia.com

What hook for product image replacement in WooCommerce shop page

Stack Overflow Asked by Toniq on January 16, 2021

What function is called when WooCommerce returns image for products in shop page? I would like to replace image with something else.

One Answer

To find out, you need look at content-product.php template source code from line 36 to 42:

    /**
     * Hook: woocommerce_before_shop_loop_item_title.
     *
     * @hooked woocommerce_show_product_loop_sale_flash - 10
     * @hooked woocommerce_template_loop_product_thumbnail - 10
     */
    do_action( 'woocommerce_before_shop_loop_item_title' );

So when looking at woocommerce_template_loop_product_thumbnail() involved hooked template function source code, you will see that it uses woocommerce_get_product_thumbnail() function:

if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {

    /**
     * Get the product thumbnail, or the placeholder if not set.
     *
     * @param string $size (default: 'woocommerce_thumbnail').
     * @param int    $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
     * @param int    $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
     * @return string
     */
    function woocommerce_get_product_thumbnail( $size = 'woocommerce_thumbnail', $deprecated1 = 0, $deprecated2 = 0 ) {
        global $product;

        $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );

        return $product ? $product->get_image( $image_size ) : '';
    }
}

So to make changes you will have to replace woocommerce_template_loop_product_thumbnail() hooked function by a custom one this way:

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_loop_product_thumbnail', 10 );
function custom_loop_product_thumbnail() {
    global $product;

    if ( $product ) {    
        // your code for image replacement below (to be echoed)
    }
}

To target shop pages only, you will use is_shop() conditional tag.

Correct answer by LoicTheAztec on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP