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

时间:2021-06-07 16:56:32

标签: wordpress woocommerce product thumbnails 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' );

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

1 个答案:

答案 0 :(得分:1)

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