How to Conditionally Hide Woocommerce Add to Cart button or Price?

Today you’ll learn how to conditionally hide Woocommerce add to cart button or price. For example, you will be able to remove add to cart button only for logged-out users. Also, I will show you how can you activate catalog mode (hide add to cart button) for all users. All this you can accomplish without any fancy “Catalogue mode” plugin.

So, let’s dive in.

How to hide WooCommerce add to cart button and price for logged-out users and show “Login to see prices” button?

First, lets take a look how to show price and add to cart button for logged-in users in a way that there is a “Login to see prices” button visible. See the example on the screenshot.

How to Conditonally Hide Woocommerce Add to Cart button or Price?

Step 1: Install Code snippets plugin or use your functions.php file

If you feel yourself comfortable modifying your theme’s functions.php file then paste the code here below inside the file. But i would suggest you to use Code Snippets plugin which will keep the solution for you even if you change the theme in the future.

So, install and activate Code Snippets plugin.

Step 2: Paste the code

Go to the Snippets >> Add new and give your snippet a meaningful title. Next, paste this code shown here below inside the code box.

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="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>';
}

Now click on Save and activate button and the prices and you’re good to go.

How to hide Woocommerce add to cart button for logged-out users?

Now, maybe you don’t want to hide the price and need to hide the add to cart button. Well, it is also easy to accomplish. Just paste this code inside the Code Snippets code box.

function catalogue_mode_for_logged_out_users(){
	$isLoggedIn = is_user_logged_in();
	if(false == $isLoggedIn){
            // Removes add to cart button to logged out users
	    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;
}

Save and activate and if everything is OK, then this should be the end result.

How to hide Woocommerce add to cart button for logged-out users?

How to hide Woocommerce add to cart button from specific user role?

This snippet here below hides add to cart button from “customer” user role. You can change the role accordingly.

/* Hide Woocommerce add to cart button from Customer user role */
add_action('wp_loaded','get_user_role');
function get_user_role(){
$current_user = wp_get_current_user();
if(count($current_user->roles)!==0){
if($current_user->roles[0]=='customer'){
add_filter('woocommerce_is_purchasable', '__return_false');
}}}

How to hide Woocommerce add to cart from all roles except customer role?

Use this snippet and all add to cart button is hidden from all user roles except Customers

/* Show add to cart button only for Customer user role */
add_action('wp_loaded','get_user_role');
function get_user_role(){
	$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) )
{
    add_filter('woocommerce_is_purchasable', '__return_true');  
} else {
    add_filter('woocommerce_is_purchasable', '__return_false');  
}
}

How to activate Woocommerce catalog mode for all users?

Sometimes there is no difference for you whether the users are logged in our out and you just need to hide the add to cart button from all users. Is so, then use this codes snippet.

add_filter( 'woocommerce_is_purchasable', '__return_false'); 
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

// If you don’t want to add the message then remove this row here and everything else below	
add_action( 'woocommerce_single_product_summary', 'optional_message', 20 );
function optional_message() {
    echo '<p class="woocommerce-message">Check back again on Monday</p>';
}

Save and activate your snippet and if everything is OK then this should be the end result.

How to activate Woocommerce catalog mode for all users

How to Hide Woocommerce Prices on the Shop and Category Pages?

Next let’s see how to hide Woocommerce prices shopwide. That means all the prices are removed both from the catalogue and single product page.

// Hide Woocommerce Prices on the Shop and Category Pages
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_price');
function woocommerce_remove_price($price){     
     return ;
}

Video: How to Conditionally Hide Woocommerce Add to Cart button or Price?

Useful tips

Here are some of my favorite Wordpress tools

Thank you for reading this article. I hope you found it helpful as you build your own websites and e-commerce sites. Here are some tools I use as a Wordpress developer and enthusiast that I hope you’ll also find helpful.

These are affiliate links, so if you do decide to use any of them, I’ll earn a commission and this helps me create these tutorials and make Youtube videos. But in all honesty, these are the exact tools that I use and recommend to everyone, even my friends and family.

Themes: For the last couple of years I have two go-to themes which I use for every kind of projects. Those two themes are Blocksy theme and Kadence Theme. On this site and my Youtube channel you’ll see a lot of tutorials I have made about them. If you would like to get a 10% discount for both of them then:

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I really like but the one that stands really out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: For getting started, I really like A2 Hosting. They have the dedicated and fast Wordpress hostinh and support has also been fast and responsive. If you’re more tech savvy and want to have a really fast managed cloud hosting then I recommend Cloudways. (Btw, this site is hosted in Cloudways.)

To see all my of most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Choose amount

Best selling plugins

Default image
Janek T.
I am a Wordpress enthusiast who has been making websites since 2011. In this site I am offering simple to follow tips on how to use Wordpress and Woocommerce. If you want to be the first to be notified about the new tutorials then please subscribe to my Youtube channel here Follow me in Twitter here
Articles: 72