Wooccommerce按类别隐藏价格+is_product_category()、is_shop()



你能帮我完成下面的代码吗?我需要隐藏特定类别的价格,但在商店和类别页面上,而不是在产品详细信息或管理上。

我需要添加is_product_category()is_shop()代码,但不知道具体在哪里。

The Snippet by jeroensormani.com

add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
if ( is_admin() ) return $price;
// Hide for these category slugs / IDs
$hide_for_categories = array( 'singles', 'albums' );
// 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' );

您可以使用以下代码隐藏shop页面和category archive页面上特定类别的价格:

add_filter('woocommerce_get_price_html', 'hide_price_on_shop_and_tax', 10, 2);
function hide_price_on_shop_and_tax($price, $product){
$hide_for_categories = array('singles', 'albums');
if ((is_shop() || is_product_category()) && has_term($hide_for_categories, 'product_cat', $product->get_id())) {
return false;
} else {
return $price;
}
}

最新更新