Replace product image by a placeholder for specific category in WooCommerce

Viewed 192

Currently I am working for a client that has a tabacco/shisha website. In the Netherlands there is a new law coming where all pictures of tabacco cannot be shown anymore on websites. So my goal is to remove the product images of this URL: https://gecona.nl/product-categorie/waterpijptabak and replace the pictures with an image placeholder.

On Google I saw that this piece of code in your functions.php should help to remove the pictures:

remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);

I added this line of code, but no images are being removed. I have searched on the internet for alternatives and fixes, but nothing works.

I would be very thankful if someone could help me with this.

1 Answers

To display a placeholder as image for specific product category defined term, use the following instead:

add_filter( 'woocommerce_product_get_image_id', 'filter_product_image_id', 999, 2 );
function filter_product_image_id( $image_id, $product ) {
    $category_terms = array('waterpijptabak'); // <== Defined terms for your category(ies)

    if ( has_term( $category_terms, 'product_cat', $product->get_id() ) ){
        return false;
    }
    return $image_id;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related