How to hide specific product categories price from cart icon dropdown

那年仲夏 提交于 2020-01-06 08:43:37

问题


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:


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


来源:https://stackoverflow.com/questions/49532567/how-to-hide-specific-product-categories-price-from-cart-icon-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!