WooCommerce

Woocommerce Hide Price & Add to Cart for Logged out users

In my previous article, we saw how to Hide Add to Cart for already purchased products. In this article i will show you how you can hide price & add to cart button if user is not logged in, or you can say hide price & add to cart for logged out users in woocommerce.

The woocommerce hide price for logged out users snippet will help you hide the prices and add to cart button for logged out users. This snippet is really useful in case you have a wholesale store and you only want to provide the price to your customers once they are logged in.

In this article, i will show you how to hide price for logged out users without using plugin. I will also provide the snippet that will let you hide the add to cart button if user is not logged in. Simply we will make it like, Login to see prices.

There are many plugins available that allow you to hide price & add to cart button but if you are not using a plugin then it becomes very hard to achieve this, so in this article i will tell you how you can achieve this using code snippet.

wpamit woocommerce hide price and add to cart for logged out users without plugin 2

If you are using woocommerce then it may be important for you to hide price & add to cart for logged out users so that they can register their account on your website and can buy products. This is a good idea to collect the email addresses of your customers so that you can send them updates about your latest products or some special offers you are giving to your customers.

Sometimes we have some products which we only want to show to our registered customers, so in that scenario, we need to hide prices and add to cart button for logged out users.

How to hide price & add to cart button for logged out users?

Before adding snippet or code in functions.php, we recommend you:

Take a full backup of your website or blog.
Be careful before editing the functions.php file. If you don’t want to edit functions.php directly, use the code snippet plugin.
We recommend you use a child theme before any customization on your website. You can use a plugin to create child theme

Also Check:
Woocommerce Hide Add to Cart for already purchased products
Woocommerce change Add to cart text if already at Cart
Woocommerce Change Add to Cart Button Text

best woocommerce theme 2023

To hide the price of a product and add to cart button if user is not logged in, we need to add a php snippet that will be checking if user is logged in or not, if he/she is not logged in then it will simply remove the price or add to cart button from all products including single product page, shop page, and widgets having products. This snippet will work with simple and variable products too.

1). Login to WordPress Admin Dashboard. Go to ‘Appearance’ then ‘Theme File Editor’. Then click on ‘Theme Functions’. This is the functions.php file of your theme.

access theme file editor
Go to Appearance then click Theme File Editor
how to change wordpress login page logo 4
Click on Theme Functions

2). Just add below code snippet in the functions.php file.

/**
 * Hide Price & add to cart for logged out users
*/
add_filter( 'woocommerce_get_price_html', 'wpamit_hide_price_add_cart_not_logged_in', 9999, 2 );
 
function wpamit_hide_price_add_cart_not_logged_in( $price, $product ) {
   if ( ! is_user_logged_in() ) { 
      $price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'wpamit' ) . '</a></div>';
      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 );
   }
   return $price;
}

You can change the text ‘Login to see prices‘ as per your requirements. I have highlighted it in the above code.

I hope you learned how to hide add to cart and prices for logged-out users without plugin. If you need any help, please comment below. I will try to resolve as soon as possible.

Leave a Reply

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