I will show you how to hide the Add to Cart button or Conditional WooCommerce Product Price. For example, you will be able to remove the add to cart button and price only for users who are not logged in. All this you can do without any plugin. Let’s find out together!

How to hide the price of a product, replace the Add to Cart button with a “Login to view price” button for users who are not logged in
First, let’s see how to show the price and add to cart button to logged in users in the “Login to view price” way.

Then enter and paste the following code into the function.php file of your theme or child theme:
add_action( 'init', 'show_price_for_logged_in_users' );
function show_price_for_logged_in_users() {
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', 'user_mesage', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'user_mesage', 11 );
}
}
function user_mesage() {
echo '<a class="button" href="'%20.%20get_permalink(%20wc_get_page_id('myaccount')%20)%20.%20'">' . __('Đăng nhập để xem giá', 'woocommerce') . '';
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span>
</div></a>
Then the result will be displayed like the screenshot below.

How to hide the add to cart button of a product for users who are not logged in?

Now, maybe you don’t want to hide the price but just hide the add to cart button. Don’t worry because it’s also easy to do. Just paste this code inside the function.php file of your theme or child theme.
function catalogue_mode_for_logged_out_users() {
$isLoggedIn = is_user_logged_in();
if ( false == $isLoggedIn ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
add_action( 'wp', 'catalogue_mode_for_logged_out_users' );
// Makes add to available for logged in users
add_filter( 'woocommerce_is_purchasable', 'keep_add_to_cart_button', 10, 2 );
function keep_add_to_cart_button( $is_purchasable, $product ) {
$isLoggedIn = is_user_logged_in();
if ( true == $isLoggedIn ) {
return true;
}
return false;
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span>
</div>
Save and activate and if everything is fine, this will be the final result.

Epilogue
So you already know how to hide WooCommerce Add to Cart or Price button.
If you find it interesting, you can follow the WordPress basics section to know more new knowledge.
Follow fanpage to receive the latest posts: Group

