The way to Conditionally Conceal Woocommerce Add to Cart button or Value?

At this time you’ll learn to conditionally cover Woocommerce add to cart button or value. For instance, it is possible for you to to take away add to cart button just for logged-out customers. Additionally, I’ll present you how are you going to activate catalog mode (cover add to cart button) for all customers. All this you possibly can accomplish with none fancy “Catalogue mode” plugin.

So, let’s dive in.

Video: The way to Conditionally Conceal Woocommerce Add to Cart button or Value?

The way to cover WooCommerce add to cart button and value for logged-out customers and present “Login to see costs” button?

First, lets have a look the way to present value and add to cart button for logged-in customers in a method that there’s a “Login to see costs” button seen. See the instance on the screenshot.

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

Step 1: Set up Code snippets plugin or use your features.php file

In the event you really feel your self snug modifying your theme’s features.php file then paste the code right here beneath contained in the file. However i’d counsel you to make use of Code Snippets plugin which is able to maintain the answer for you even in case you change the theme sooner or later.

So, set up and activate Code Snippets plugin.

Step 2: Paste the code

Go to the Snippets >> Add new and provides your snippet a significant title. Subsequent, paste this code proven right here beneath contained in the code field.

<span function="button" tabindex="0" data-code="add_action( 'init', 'show_price_for_logged_in_users' );

operate 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 );
}
}
operate user_mesage() {
echo '<a category="button" href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see costs', 'theme_name') . '

add_action( 'init', 'show_price_for_logged_in_users' );
  
operate 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 );
   }
}
operate user_mesage() {
   echo '<a category="button" href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see costs', 'theme_name') . '</a>';
}

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

The way to cover Woocommerce add to cart button for logged-out customers?

Now, perhaps you don’t need to cover the worth and wish to cover the add to cart button. Effectively, it is usually straightforward to perform. Simply paste this code contained in the Code Snippets code field.

operate catalogue_mode_for_logged_out_users(){
	$isLoggedIn = is_user_logged_in();
	if(false == $isLoggedIn){
            // Removes add to cart button to logged out customers
	    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 obtainable for logged in customers
add_filter('woocommerce_is_purchasable', 'keep_add_to_cart_button', 10, 2);
operate 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 every little thing is OK, then this must be the top consequence.

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

The way to cover Woocommerce add to cart button from particular consumer function?

This snippet right here beneath hides add to cart button from “buyer” consumer function. You’ll be able to change the function accordingly.

/* Conceal Woocommerce add to cart button from Buyer consumer function */
add_action('wp_loaded','get_user_role');
operate get_user_role(){
$current_user = wp_get_current_user();
if(rely($current_user->roles)!==0){
if($current_user->roles[0]=='buyer'){
add_filter('woocommerce_is_purchasable', '__return_false');
}}}

The way to show Woocommerce add to cart button for particular consumer function?

Use this snippet to cover Woocommerce add to cart button from particular consumer function (buyer, for instance).

/* Present add to cart button just for Buyer consumer function */
add_action('wp_loaded','get_user_role');
operate get_user_role(){
	$consumer = wp_get_current_user();
if ( in_array( 'buyer', (array) $consumer->roles ) )
{
    add_filter('woocommerce_is_purchasable', '__return_true');  
} else {
    add_filter('woocommerce_is_purchasable', '__return_false');  
}
}

The way to cover Woocommerce add to cart button for a particular product?

Check out the code beneath. It will cover Woocommerce add to cart button for a particular product and the product has an ID quantity 80. So, simply change your product ID quantity and also you’re good to go.

Now, the way to discover out your product quantity? Open your product for modifying and try the URL. It appears to be like one thing like this: https://yoursite.coml/wp-admin/put up.php?put up=1416&motion=edit

Did you discover 1416 in it? That’s your ID quantity.

<span function="button" tabindex="0" data-code="// Conceal Woocommerce add to cart button for a particular product
add_filter('woocommerce_is_purchasable', 'hide_add_to_cart_for_specific_product', 23, 2 );
operate

// Conceal Woocommerce add to cart button for a particular product
add_filter('woocommerce_is_purchasable', 'hide_add_to_cart_for_specific_product', 23, 2 );
operate <meta charset="utf-8">hide_add_to_cart_for_specific_product( $is_purchasable, $product ) {
	$consumer = wp_get_current_user();
  if( in_array( 'buyer',  (array) $consumer->roles ) && ($product->get_id() == 80) ) { // Change your product ID quantity right here
    return false;
  } 
  return $is_purchasable;
}

The way to cover Woocommerce add to cart button just for particular product and just for logged out customers?

Let’s think about that I’ve one product in my store that must be seen to all customers however solely logged in customers are ready so as to add it to the cart? Effectively, on this case let’s have a look on the way to cover Woocommerce particular product add to cart button for logged out customers.

NB! As soon as once more, you could change product ID quantity accordingly.

<span function="button" tabindex="0" data-code="// Conceal Woocommerce particular product add to cart button for logged out customers
add_filter('woocommerce_is_purchasable', '<meta charset="utf-8">hide_add_to_cart_for_logged_out_users', 23, 2 );
operate <meta charset="utf-8">hide_add_to_cart_for_logged_out_users( $is_purchasable, $product ) {
$consumer = wp_get_current_user();
if( !is_user_logged_in() && ($product->get_id() == 80) ) { //

// Conceal Woocommerce particular product add to cart button for logged out customers
add_filter('woocommerce_is_purchasable', '<meta charset="utf-8">hide_add_to_cart_for_logged_out_users', 23, 2 );
operate <meta charset="utf-8">hide_add_to_cart_for_logged_out_users( $is_purchasable, $product ) {
	$consumer = wp_get_current_user();
  if( !is_user_logged_in() && ($product->get_id() == 80) ) { // <meta charset="utf-8">Change your product ID quantity right here
    return false;
  } 
  return $is_purchasable;
}

The way to cover Woocommerce add to cart button just for a particular product and just for a particular consumer function?

Subsequent situation: I’ll cover add to cart bytton just for particular product (with ID quantity 80) and for a particular consumer function (Course member). All different customers (even logged out customers) are ready to make use of add to cart button.

// Conceal Woocommerce add to cart button just for a particular product and just for a particular consumer function
add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_product_80', 23, 2 );
operate set_catalog_mode_on_for_product_80( $is_purchasable, $product ) {
	$consumer = wp_get_current_user();
  if( in_array( 'course_member',  (array) $consumer->roles ) && ($product->get_id() == 80) ) { // you possibly can set a number of circumstances right here
    return false;
  } 
  return $is_purchasable;
}

The way to activate Woocommerce catalog mode for all customers?

Typically there isn’t a distinction for you whether or not the customers are logged in our out and also you simply want to cover the add to cart button from all customers. Is so, then use this codes snippet.

<span function="button" tabindex="0" data-code="add_filter( 'woocommerce_is_purchasable', '__return_false');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

// In the event you don’t need to add the message then take away this row right here and every little thing else beneath
add_action( 'woocommerce_single_product_summary', 'optional_message', 20 );
operate optional_message() {
echo '<p class="woocommerce-message">Examine again once more on Monday

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

// In the event you don’t need to add the message then take away this row right here and every little thing else beneath	
add_action( 'woocommerce_single_product_summary', 'optional_message', 20 );
operate optional_message() {
    echo '<p class="woocommerce-message">Examine again once more on Monday</p>';
}

Save and activate your snippet and if every little thing is OK then this must be the top consequence.

How to activate Woocommerce catalog mode for all users

The way to Conceal Woocommerce Costs on the Store and Class Pages?

Subsequent let’s see the way to cover Woocommerce costs shopwide. Which means all the costs are eliminated each from {the catalogue} and single product web page.

// Conceal Woocommerce Costs on the Store and Class Pages
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_price');
operate woocommerce_remove_price($value){     
     return ;
}

Helpful suggestions

  • The way to Show the Low cost Share on the Sale Badge?
  • Kadence Theme tutorial: The way to Create Complicated Headers with Kadence Professional?
  • Woocommerce: The way to Present Product Already in Cart button?

Leave a Comment

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

Shopping Cart
Scroll to Top