在 WooCommerce 购物车中隐藏特定产品类别的缩略图

Hide thumbnail for specific product categories in WooCommerce cart

我正在尝试隐藏 WooCommerce 购物车中某些类别的图片。我发现添加此代码会从购物车中删除所有缩略图:

add_filter( 'woocommerce_cart_item_thumbnail', '__return_false' );

我正在尝试(但失败了)使用以下代码仅针对某些类别删除它。

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' );

我不确定我哪里做错了,如果有人有提示,那就太好了!

woocommerce_cart_item_thumbnail 过滤器挂钩包含 3 个参数, 第二个是 $cart_item,因此您可以将 $cart_item['product_id']has_term()

结合使用

所以你得到:

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 );