user994585 user994585 - 2 months ago 19
PHP Question

Replacing Image on Woocommerce Shop Page using hooks

I'm trying to use hooks to remove the product thumbnail from the shop/catalog page in Woocommerce and then replace it with my own custom image.

add_action
works as expected and displays the text, but the
remove_action
does not remove the product thumbnail.
Here is the page related web site page.

What I am doing wrong?

Below is the code I'm using:

// Remove product images from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );

//Add custom code to replace product thumbnail
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumb', 10 );

if ( ! function_exists( 'woocommerce_template_loop_product_thumb' ) ) {
function woocommerce_template_loop_product_thumb() {
echo "testing";
}
}

Answer

You just need to fire this using woocommerce_init hook and this way it just works.

Here is the code:

function replacing_template_loop_product_thumbnail() {
    // Remove product images from the shop loop
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
    // Adding something instead
    function wc_template_loop_product_replaced_thumb() {
        echo "TEST TEST";
    }
    add_action( 'woocommerce_before_shop_loop_item_title', 'wc_template_loop_product_replaced_thumb', 10 );
}
add_action( 'woocommerce_init', 'replacing_template_loop_product_thumbnail');

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and fully functional.