在Woocommerce中基于产品类别隐藏价格 [英] Hide Price based on product category in Woocommerce

查看:64
本文介绍了在Woocommerce中基于产品类别隐藏价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我试图根据类别隐藏归档页面和单个产品页面上的产品,但是这种情况似乎不起作用,并且无论我是否设置了类别,都只是隐藏了所有价格

In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not

add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );

function woocommerce_remove_prices( $price, $product ) {
     if(is_product_category('sold')){   
        $price = '';
        return $price;
     } 
}

推荐答案

要使代码正常工作,您需要为单个产品页面使用 has_term()条件函数,并且您需要始终返回在 if 语句之外的结尾处的价格:

To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:

add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
    if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
        $price = '';

    return $price;
}

有效!但这不会删除所选的产品差异价格,并且不会在您仍然有添加到购物车按钮的任何位置.

It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.

代码进入您的活动子主题(或活动主题)的function.php文件中.

相反,您可以使用以下命令删除该特定产品类别上的所有价格,数量按钮和购物车按钮:

Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:

// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
    global $product;

    if( is_product_category('sold') ):

    // Hide prices
    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    // Hide add-to-cart button
    remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );

    endif;
}

// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
    global $product;

    if( has_term( 'sold', 'product_cat', $product->get_id() ) ):

    // Hide prices
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

    // Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
    if( ! $product->is_type('variable') ){
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
    } else {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    }

    endif;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

经过测试,可以正常工作.

Tested and works.

这篇关于在Woocommerce中基于产品类别隐藏价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆