【问题标题】:Hide thumbnail for specific product categories in WooCommerce cart在 WooCommerce 购物车中隐藏特定产品类别的缩略图
【发布时间】:2021-08-24 18:49:53
【问题描述】:

我正在尝试在 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' );

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

【问题讨论】:

    标签: wordpress woocommerce product thumbnails cart


    【解决方案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 );
    

    【讨论】:

    • 非常感谢,效果很好!
    • @AlphaPixa 感谢您接受我的回答。但是,要知道您也可以选择upvote 这个答案。另请参阅:How can I upvote answers and comments? 提前致谢!致以最诚挚的问候
    猜你喜欢
    • 2019-01-12
    • 2023-04-07
    • 2019-01-31
    • 1970-01-01
    • 2019-01-27
    • 2018-07-11
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多