0

I have next code:

add_action('init', 'bbloomer_hide_price_add_cart_not_logged_in');

function bbloomer_hide_price_add_cart_not_logged_in() {
 if (!is_user_logged_in()) {
   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', 'bbloomer_print_login_to_see', 31);
   add_action('woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11);
 }
}

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

This code is use for woocommerce site, and hides prices for not loged in users. It works perfectly. I want to hide price of products from a specific category. I supose that i have to change this part of the code in: if ( !is_user_logged_in() && is_category('my category') ) { ... }, theoretically these changes should work, but i don't understand why i don't get the desired result. Who knows why the code doesn't work?

  • Will you hide the price for non logged in users for specific product category? – Chinmoy Kumar Paul Feb 16 at 11:34
  • You will replace this if (!is_user_logged_in()) { with if (!is_user_logged_in() && is_product_category( ENTER CAT ID HERE ) ) { – Chinmoy Kumar Paul Feb 16 at 11:35
  • Yes, i want to do this. – Asking Feb 16 at 11:35
  • @Chinmoy Kumar Paul i also tried this code, but it doesn't work – Asking Feb 16 at 11:42
  • @FabrizioMele shared the code. You can try once. – Chinmoy Kumar Paul Feb 16 at 11:44
0

The is_category() method checks whether the current query is for an existing category archive page. Being woocommerce you should use the is_product_category() method instead, and to check for single product the has_term($slug, 'product_cat', $product_id) method.

This code will remove prices from archive pages and single product 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;
}

Reference: Hide Price based on product category in Woocommerce

  • how to check if the user is not loged in and in the same time if this user is not loged in, to hide for the same user the price for a certain category ? – Asking Feb 16 at 20:36
  • You just need to and this condition to the if in both functions: !is_user_logged_in() – Fabrizio Mele Feb 16 at 20:38
  • i changed next: if( is_product_category('sold') ) to if( is_product_category('mycategory') && !is_user_logged_in()): and if( has_term( 'sold', 'product_cat', $product->get_id() ) ): to if( has_term( 'mycategory', 'product_cat', $product->get_id() ) && !is_user_logged_in() ):, but my code still doesn't work. What could be the problem? – Asking 36 mins ago Delete – Asking Feb 16 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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