最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - is_product_category('Services') not working

programmeradmin1浏览0评论

I'd like to hide the "add to cart" & price for WooCommerce products in the 'Services' category.

I've tried the following code in my child theme's functions.php, but / still displays "add to cart" & price.

Help appreciated.

function insight_hide_price_add_cart_not_logged_in() {   
    if (! is_user_logged_in()) {
        if (is_product_category('Services')) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 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', 11 );
        }
    }
}

function insight_print_register_to_see() {
echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Register to see prices', 'theme_name') . '</a>';
}

add_action( 'init', 'insight_hide_price_add_cart_not_logged_in' );

I'd like to hide the "add to cart" & price for WooCommerce products in the 'Services' category.

I've tried the following code in my child theme's functions.php, but https://pureblissmoonhealer/product-category/services/ still displays "add to cart" & price.

Help appreciated.

function insight_hide_price_add_cart_not_logged_in() {   
    if (! is_user_logged_in()) {
        if (is_product_category('Services')) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 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', 11 );
        }
    }
}

function insight_print_register_to_see() {
echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Register to see prices', 'theme_name') . '</a>';
}

add_action( 'init', 'insight_hide_price_add_cart_not_logged_in' );
Share Improve this question asked Nov 4, 2019 at 2:30 InsightInsight 1071 silver badge14 bronze badges 1
  • The init hook is too early for is_product_category(). Try wp or a later hook. – Sally CJ Commented Nov 4, 2019 at 11:30
Add a comment  | 

2 Answers 2

Reset to default 0

You can use below code to hide add to cart & price for WooCommerce products in the 'Services' category.

// 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>';
}

i have tested this code and working fine for me. let me know if this works for you.

Hope this method will helpful for you.Resource form https://www.skyverge/blog/checking-woocommerce-cart-contains-product-category/

function insight_hide_price_add_cart_not_logged_in() {
    $cat_check = false;
    if (! is_user_logged_in()) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            if ( has_term( 'Services', 'product_cat', $product->id ) ) {
                $cat_check = true;
                break;
            }
        }  
        if ( $cat_check ) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 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', 11 );
        }   
    }
}

function insight_print_register_to_see() {
    echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Register to see prices',     'theme_name') . '</a>';
}

add_action( 'init', 'insight_hide_price_add_cart_not_logged_in' );
发布评论

评论列表(0)

  1. 暂无评论