Php 隐藏产品价格并禁用Woocommerce中特定产品类别的“添加到购物车”

Php 隐藏产品价格并禁用Woocommerce中特定产品类别的“添加到购物车”,php,wordpress,woocommerce,price,taxonomy-terms,Php,Wordpress,Woocommerce,Price,Taxonomy Terms,我正在寻找正确的代码来隐藏Woocommerce中某些特定类别的价格 我已经有了在de single product页面上隐藏价格的代码: add_action( 'wp', 'remove_prices_based_on_category' ); function remove_prices_based_on_category() { // On product single pages if ( is_product() ) { remove_product

我正在寻找正确的代码来隐藏Woocommerce中某些特定类别的价格

我已经有了在de single product页面上隐藏价格的代码:

add_action( 'wp', 'remove_prices_based_on_category' );
function remove_prices_based_on_category() {
    // On product single pages 
    if ( is_product() ) {
        remove_product_price( get_the_ID() );
    }
}

function return_custom_price( $price, $instance ) {
    $price = '<span style="color:red; font-size:12px;">Call our office <strong>516.695.3110</strong> for prices.</span>';
    return $price; 
}

add_action( 'woocommerce_before_shop_loop_item', 'remove_product_price', 5, 1 ); // for each product on product listing page/shop page.
function remove_product_price( $product_id ) {
    $product_id  = get_the_ID();
    $hidden_price_category_ids = array( '27419','27421' ); // Add Product Category IDs for which the product price should be hidden.
    $product_cat_ids  = get_the_terms( $product_id, 'product_cat' ); // Getting all categories for this product.
    $cat_ids = wp_list_pluck( $product_cat_ids, 'term_id' ); // Getting all category ids for this product.   
    $result = array_intersect( $hidden_price_category_ids, $cat_ids ); // Will match hidden price categories with product categories and the cat id in the array.

    // If a hidden price category is found
    if( !empty($result) ) {
        add_filter( 'woocommerce_get_price_html', 'return_custom_price', 10, 2 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    } else {
        remove_filter( 'woocommerce_get_price_html', 'return_custom_price', 10, 2 );
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    }
}
add_action('wp','remove_prices_-based_-on_-category');
函数根据类别删除价格{
//在产品单页上
if(is_product()){
删除产品价格(获取ID());
}
}
函数返回\自定义\价格($price,$instance){
$price='致电我们的办公室了解价格。516.695.3110;
返回$price;
}
添加动作('woocommerce'u before\u shop\u loop\u item','remove\u product\u price',5,1);//对于产品列表页/商店页上的每个产品。
函数删除产品价格($product\U id){
$product_id=获取_id();
$hidden_price_category_id=array('27419','27421');//添加应隐藏产品价格的产品类别ID。
$product_cat_ids=获取_术语($product_id,'product_cat');//获取此产品的所有类别。
$cat_id=wp_list_pull($product_cat_id,'term_id');//获取此产品的所有类别id。
$result=array_intersect($hidden_price_category_id,$cat_id);//将隐藏的价格类别与数组中的产品类别和cat id相匹配。
//如果发现隐藏的价格类别
如果(!空($result)){
添加过滤器('woocommerce\u get\u price\u html','return\u custom\u price',10,2);
删除操作('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u add\u to\u cart',30);
删除操作('woocommerce'u在'u shop'u loop'u item'之后,'woocommerce'模板'u loop'u将'u添加到'u cart');
}否则{
删除过滤器('woocommerce\u get\u price\u html','return\u custom\u price',10,2);
添加操作('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u add\u to\u cart',30);
添加操作('woocommerce'u在'u shop'u loop'u项目之后,'woocommerce'模板'u loop'u添加'u到'u cart');
}
}

如何为WooCommerce归档页面执行此操作?

您现有的代码复杂、未完成,而且不太方便。请尝试以下方法,这些方法将适用于单个产品页面,也适用于存档页面(作为商店页面)

它处理任何类型的产品,包括可变产品及其变体

对于已定义的产品类别,它将替换价格并禁用相关产品上的“添加到购物车”按钮

守则:

// Custom conditional function that check for specific product categories
function check_for_defined_product_categories( $product_id ) {
    // HERE your Product Categories where the product price need to be hidden.
    $targeted_terms = array( '27419','27421' ); // Can be term names, slugs or Ids

    return has_term( $targeted_terms, 'product_cat', $product_id );
}

// Custom function that replace the price by a text
function product_price_replacement(){
    return '<span style="color:red; font-size:12px;">' . sprintf( __( "Call our office %s for prices."), '<strong>516.695.3110</strong>' ) . '</span>';
}

// Replace price by a text (conditionally)
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ){
    if( check_for_defined_product_categories( $product->get_id() ) ) {
        $price = product_price_replacement();
    }
    return $price;

}

// Hide prices and availiability on product variations (conditionally)
add_filter( 'woocommerce_available_variation', 'filter_available_variation_callback', 10, 3 ); // for Variations
function filter_available_variation_callback( $args, $product, $variation ) {
    if( check_for_defined_product_categories( $product->get_id() ) ) {
        $args['price_html'] = '';
        $args['availability_html'] = '';
    }
    return $args;
}

// Disable add to cart button (conditionally)
add_filter( 'woocommerce_variation_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
    $product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();

    if( check_for_defined_product_categories( $product_id ) ) {
        $purchasable = false;
    }
    return $purchasable;
}
//检查特定产品类别的自定义条件函数
定义的产品类别的功能检查($product\U id){
//这里是需要隐藏产品价格的产品类别。
$targeted_terms=array('27419','27421');//可以是术语名、slug或id
退货有条款($product\U terms,$product\U cat',$product\U id);
}
//用文本替换价格的自定义函数
功能产品\价格\更换(){
返回'.sprintf(uuuu(“致电我们的办公室%s了解价格)”,'516.695.3110)';
}
//用文本替换价格(有条件)
添加过滤器('woocommerce\u get\u price\u html','filter\u get\u price\u html\u callback',10,2);
函数过滤器\u获取\u价格\u html\u回调($price,$product){
如果(检查定义的产品类别($product->get\u id())){
$price=产品价格更换();
}
返回$price;
}
//隐藏产品变化的价格和责任(有条件)
添加过滤器('woocommerce'u available'u variation'、'filter'u available'u variation'u callback',10,3);//变化
函数筛选器\可用\变量\回调($args、$product、$variation){
如果(检查定义的产品类别($product->get\u id())){
$args['price_html']='';
$args['availability\u html']='';
}
返回$args;
}
//禁用“添加到购物车”按钮(有条件)
添加过滤器('woocommerce\u变体\u可购买','woocommerce\u可购买\u过滤器\u回调',10,2);
添加过滤器(“woocommerce\u可购买”,“woocommerce\u可购买\u过滤器\u回调”,10,2);
函数是可购买的过滤器回调($purchasable,$product){
$product\U id=$product->get\U parent\U id()>0?$product->get\U parent\U id():$product->get\U id();
if(检查定义的产品类别($product\U id)){
$purchasable=false;
}
返回$purchable;
}

代码进入活动子主题(或活动主题)的functions.php文件。测试和工作。

作为一个新用户,您可以从根本上解释StackOverFlow的工作原理。