Remove Add To Cart Softexpert

How to 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.

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *