You may want to force your user to login to see the price and add to cart of product in WordPress. Sometimes it is good for the e-commerce business that the only real customer will see the price of the product and add to cart button of the product. If user not logged in then he will see a message like “Login to see the price”.

hide price and add to cart for logged out user in Woocommerce

But why you may need to hide the price and add to cart for logged out user in Woocommerce?

Well, If you have an e-commerce business which is made by WordPress and Woocommerce then you may want to deal with only real customers or users. You may want to show your product price who are really interested or who are logged in the system. Sometimes it is helpful to get real customer or user. And it may easy to sort out the users and may get their details like email and phone. You may send them an email with a special offer or discount offer.
You can easily hide the price and add to cart for logged out user in Woocommerce. Here is the step to step guide and explanation.
Step-1: If you haven’t any FTP access of your website then make an FTP account for it.
Step-2: Go to your theme folder and select the functions.php file. Download it or click right button on functions.php file select “view/edit”.

 

hide price and add to cart for logged out user in Woocommerce - functions.php

 

Step-3: paste the following code in functions.php file ( It is recommended to add the code in the end of the file to edit or find easily ), save it and upload it. You are done.

/*Hide the price and add to cart for logged out user in Woocommece*/

add_action( 'init', 'WP_expert_hide_price_and_add_to_cart' );
 
function WP_expert_hide_price_and_add_to_cart() { 
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', 'WP_expert_to_see_the_price', 31 );
 add_action( 'woocommerce_after_shop_loop_item', 'WP_expert_to_see_the_price', 11 );
}
}
 
function WP_expert_to_see_the_price() {
echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see the price', 'your_theme_name') . '</a>';
}

 

Now log out or browse the site in the new browser, you will see there is no price and add to cart and the message “Login to see the price”. The Final result is:

 

Note: Be sure that, you know what you going to do and if don’t have any idea about woocommerce hook and functionality then take a break and read out about woocommerce and how it works.