您可以使用以下代码隐藏add to cart
&;price
适用于“服务”类别的WooCommerce产品。
// Specific product category archive and shop pages
add_action( \'woocommerce_after_shop_loop_item_title\', \'hide_loop_product_prices\', 1 );
function hide_loop_product_prices(){
global $product;
if (! is_user_logged_in()) {
if( is_product_category(\'services\') || has_term( \'services\', \'product_cat\', $product->get_id() ) ){
// 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\', 10 );
add_action( \'woocommerce_single_product_summary\', \'insight_print_register_to_see\', 31 );
add_action( \'woocommerce_after_shop_loop_item\', \'insight_print_register_to_see\', 99 );
}
else
{
add_action( \'woocommerce_after_shop_loop_item\', \'woocommerce_template_loop_add_to_cart\', 10 );
add_action(\'woocommerce_after_shop_loop_item_title\', \'woocommerce_template_loop_price\', 10 );
remove_action(\'woocommerce_single_product_summary\', \'insight_print_register_to_see\', 31);
remove_action(\'woocommerce_after_shop_loop_item\',\'insight_print_register_to_see\', 99 );
}
}
}
// Single product pages
add_action( \'woocommerce_single_product_summary\', \'hide_single_product_prices\', 1 );
function hide_single_product_prices(){
global $product;
if (! is_user_logged_in()) {
if(is_product_category(\'services\') || has_term( \'services\', \'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 );
}
add_action( \'woocommerce_single_product_summary\', \'insight_print_register_to_see\', 31 );
endif;
}
}
function insight_print_register_to_see() {
echo \'<a href="\' . get_permalink(wc_get_page_id(\'myaccount\')) . \'">\' . __(\'Login/Register to see Price\', \'theme_name\') . \'</a>\';
}
我已经测试了这段代码,并且工作得很好。如果这对你有用,请告诉我。