我有两个类别的产品(个人和企业)。我只想从业务类别中定价。如果用户登录,则用户将显示价格。我尝试过,但仅适用于所有类别的产品,而不适用于特定类别的产品。请帮忙。预先谢谢你。
//Hide specific product price for registered user only
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_user_logged_in()) {
if( is_product_category('business') || has_term( 'business', 'product_cat', $product->get_id() ) ) {
//remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
} else {
return $price;
}
}
}
答案 0 :(得分:0)
抱歉,检查用户登录的逻辑错误。我必须检查用户是否未登录。
//Hide specific product price for registered user only
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_user_logged_in()) {
if( is_product_category('business') || has_term( 'business', 'product_cat', $product->get_id() ) ) {
//remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
} else {
return $price;
}
} }
答案 1 :(得分:0)
尝试这个:
if(has_term( 'business', 'product_cat', $product->get_id() ) ) {
//remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
} else {
return $price;
}