隐藏购物车页面

时间:2018-03-27 22:34:20

标签: php wordpress woocommerce content-management-system cart

我正在尝试隐藏产品页面+购物车中的特定类别价格,我正在使用我在其他地方找到的代码段。

<?php 
add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
    if ( is_admin() ) return $price;
    // Hide for these category slugs / IDs
    $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; // Return original price
}, 10, 2 );
add_filter( 'woocommerce_cart_item_price', '__return_false' );
add_filter( 'woocommerce_cart_item_subtotal', '__return_false' );
 ?>

我面临的问题是,此代码隐藏了购物车中的所有类别价格。它适用于产品页面,但不适用于购物车。任何人都可以帮我改变代码中的内容?我是WordPress编码的新手。

非常感谢。

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题,将隐藏您的类别列表中每个产品的购物车页面上的价格和总数。

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