Search code examples
wordpresswoocommerceproductthumbnailscart

Hide thumbnail for specific product categories in WooCommerce cart


I'm trying to hide the image in the WooCommerce Cart for certain categories. I found that adding this code removes all thumbnails from the Cart:

add_filter( 'woocommerce_cart_item_thumbnail', '__return_false' );

I'm trying (and failing), to only remove this for certain categories, using the following code.

function WooCartImage($woocommerce_cart_item_thumbnail) {
if ( is_product_category(63) ) {
    $woocommerce_cart_item_thumbnail = '__return_false';
}

return $woocommerce_cart_item_thumbnail;
}
add_filter( 'woocommerce_cart_item_thumbnail', 'WooCartImage' );

I'm not sure where I'm going wrong with this and if anyone has a hint, that would be great!


Solution

  • The woocommerce_cart_item_thumbnail filter hook contains 3 arguments, the 2nd is $cart_item, so you can use $cart_item['product_id'] in combination with has_term()

    So you get:

    function filter_woocommerce_cart_item_thumbnail( $product_image, $cart_item, $cart_item_key ) {
        // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
        $categories = array( 63, 15, 'categorie-1' );
        
        // Has term (product category)
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $product_image = '';
        }
        
        return $product_image;
    }
    add_filter( 'woocommerce_cart_item_thumbnail', 'filter_woocommerce_cart_item_thumbnail', 10, 3 );