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