How to hide specific product categories price from

2019-08-20 05:50发布

I have some product categories which I don't to show the price to the user. Successfully hide the price from the product pages and also from the cart page but the price still showing in the cart icon when the user hovers over the icon and a dropdown list appear of the products that are in the cart.

Anyone know how to fix this? Any help will be appreciated.This is the code that I am using to hide the price for the specific category.

add_filter('woocommerce_cart_item_price', 'hide_woocommerce_cart_item_price', 10, 3);
add_filter('woocommerce_cart_item_subtotal', 'hide_woocommerce_cart_item_price', 10, 3);

function hide_woocommerce_cart_item_price( $price,  $cart_item, $cart_item_key ) {
    $product = wc_get_product($cart_item['product_id']);
    $hide_for_categories = array( 'berkley','cotton-lite','kinna','linen','luster','nairobi','panama','plisse','prints','sequoia','shantung','brocade','boucle','dover','lite-out','lite-out-duplex','moire','sheerweave-blackout','sutton','texas-green','windsor','sheer','sheerweave-3-5-10','sheerweave-specialty');
    // Don't show price when its in one of the categories
    if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
        return '';
    }
    return $price; 
}

1条回答
男人必须洒脱
2楼-- · 2019-08-20 06:55

This should hide the subtotal in the cart hover but it's dependent on the theme you are using.

add_filter('woocommerce_cart_product_subtotal', 'hide_woocommerce_cart_product_subtotal', 10, 4);

function hide_woocommerce_cart_product_subtotal( $sub_total, $product, $quantity, $cart ) {
    $hide_for_categories = array( 'berkley','cotton-lite','kinna','linen','luster','nairobi','panama','plisse','prints','sequoia','shantung','brocade','boucle','dover','lite-out','lite-out-duplex','moire','sheerweave-blackout','sutton','texas-green','windsor','sheer','sheerweave-3-5-10','sheerweave-specialty');
    // Don't show price when its in one of the categories
    if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
        return '';
    }

    return $sub_total;
}
查看更多
登录 后发表回答