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.

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

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 display Woocommerce add to cart button for specific user role?

Use this snippet tohide Woocommerce add to cart button from specific user role (customer, for example).

/* 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 hide Woocommerce add to cart button for a specific product?

Take a look at the code below. This will hide Woocommerce add to cart button for a specific product and the product has an ID number 80. So, just change your product ID number and you’re good to go.

Now, how to find out your product number? Open your product for editing and take a look at the URL. It looks something like this: https://yoursite.coml/wp-admin/post.php?post=1416&action=edit

Did you notice 1416 in it? That’s your ID number.

// Hide Woocommerce add to cart button for a specific product
add_filter('woocommerce_is_purchasable', 'hide_add_to_cart_for_specific_product', 23, 2 );
function <meta charset="utf-8">hide_add_to_cart_for_specific_product( $is_purchasable, $product ) {
	$user = wp_get_current_user();
  if( in_array( 'customer',  (array) $user->roles ) && ($product->get_id() == 80) ) { // Change your product ID number here
    return false;
  } 
  return $is_purchasable;
}

How to hide Woocommerce add to cart button only for specific product and only for logged out users?

Let’s imagine that I have one product in my shop that should be visible to all users but only logged in users are able to add it to the cart? Well, in this case let’s take a look on how to hide Woocommerce specific product add to cart button for logged out users.

NB! Once again, you need to change product ID number accordingly.

// Hide Woocommerce specific product add to cart button for logged out users
add_filter('woocommerce_is_purchasable', '<meta charset="utf-8">hide_add_to_cart_for_logged_out_users', 23, 2 );
function <meta charset="utf-8">hide_add_to_cart_for_logged_out_users( $is_purchasable, $product ) {
	$user = wp_get_current_user();
  if( !is_user_logged_in() && ($product->get_id() == 80) ) { // <meta charset="utf-8">Change your product ID number here
    return false;
  } 
  return $is_purchasable;
}

How to hide Woocommerce add to cart button only for a specific product and only for a specific user role?

Next scenario: I will hide add to cart bytton only for specific product (with ID number 80) and for a specific user role (Course member). All other users (even logged out users) are able to use add to cart button.

// Hide Woocommerce add to cart button only for a specific product and only for a specific user role
add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_product_80', 23, 2 );
function set_catalog_mode_on_for_product_80( $is_purchasable, $product ) {
	$user = wp_get_current_user();
  if( in_array( 'course_member',  (array) $user->roles ) && ($product->get_id() == 80) ) { // you can set multiple conditions here
    return false;
  } 
  return $is_purchasable;
}

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

Useful tips

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.

Donation Form (#2)

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

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 a 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 a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands 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: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

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

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 134