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

Flo*_*ter 2 wordpress code-snippets woocommerce

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

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

该片段由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' );
Run Code Online (Sandbox Code Playgroud)

Mok*_*ess 5

您可以使用以下代码隐藏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;
    }
}
Run Code Online (Sandbox Code Playgroud)