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 https://pureblissmoonhealer.com/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' );
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

2 Answers 2

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.

11
  • Thanks Chetan. I've added this code to the child theme functions.php, replacing the existing code, but the prices remain @ the category in question.
    – Insight
    Commented Nov 5, 2019 at 2:47
  • Still seeking help please.
    – Insight
    Commented Nov 14, 2019 at 2:01
  • i have check your website it is working fine. prnt.sc/pwo6th please open your website in incognito window without login Commented Nov 14, 2019 at 2:55
  • Did you check ? here is other category which show price and add to cart prnt.sc/pwoysh. and here is service category which price and add to cart button hidden prnt.sc/pwoyyj. means it is working fine. Commented Nov 14, 2019 at 4:41
  • The product page still has the price unfortunately.
    – Insight
    Commented Nov 15, 2019 at 7:21
0

Hope this method will helpful for you.Resource form https://www.skyverge.com/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' );
4
  • Thanks for your answer. Does that need a product to be in the cart first? We need the price and add to cart to be hidden before they do anything, including adding to cart.
    – Insight
    Commented Nov 4, 2019 at 5:05
  • Have you used is_category() function instead of is_product_category() Commented Nov 4, 2019 at 5:27
  • I definitely used is_product_category
    – Insight
    Commented Nov 4, 2019 at 7:29
  • Still seeking help please.
    – Insight
    Commented Nov 14, 2019 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.