Are you looking for a way to hide the price and Add to Cart button for logged out users? In this brief post, you will learn how you may force users to log in to see prices and add products to the cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. There are many ways that you can let customers log into your WooCommerce online store.
WooCommerce Hide Price and Add to Cart for Logged Out
WooCommerce does not have built-in functionality to hide the price and Add to Cart button, but I will share a custom PHP code snippet for this purpose. Additionally, there are many different types of WooCommerce login, which you can add to your store.
Different Types of WooCommerce Login
The term ‘WooCommerce Login’ is the process where customers log into your WooCommerce store. However, this is quite a vague term, and there are many ways to set up the WooCommerce login. Moreover, it is important to note that you should only use the one that fits your WooCommerce store description.
Here are some of the different types of WooCommerce login:
- Public WooCommerce store with guest checkout – Most WooCommerce stores use this type of setup. This setup allows anyone to browse the products in your store. Moreover, there is no hidden content. Customers can purchase products as a guest without having to log in. Additionally, customers can optionally create an account during the Checkout.
- Public WooCommerce store with mandatory user accounts – Other WooCommerce stores require visitors to register or login to their accounts to purchase products. In this tutorial, you will learn how to set it up.
- Customer portal with protected product categories – This setup uses password protection on different parts of your WooCommerce store. Moreover, this setup restricts specific users or user roles. However, the public can view the products.
- Private WooCommerce store – this type of setup hides the whole WooCommerce store from public view so that customers must log in to access products.
With that said, let us look at how you can hide the price and Add to Cart button for logged out users.
Steps to Hide Price and Add to Cart for Logged Out Users
Before we look at the steps, the image below shows how the prices and the Add to Cart button are displayed by default, even for logged out users:
Here are the steps that you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to hide price and add to cart for logged out users.
- Click anywhere on the text editor and scroll to the bottom of the functions.php file. Paste the code at the end:
/** * Hide Price & Add to Cart for Logged Out Users */ add_action( 'init', 'njengah_hide_price_add_cart_not_logged_in' ); function njengah_hide_price_add_cart_not_logged_in() { 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', 'njengah_print_login_to_see', 31 ); add_action( 'woocommerce_after_shop_loop_item', 'njengah_print_login_to_see', 11 ); } } function njengah_print_login_to_see() { echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>'; }
- Click on the Save changes button at the bottom of the text editor screen.
- You have successfully added the code snippet required to hide price and add to cart for logged out users. This will be the outcome on the front end:
Conclusion
In this brief tutorial, I have shared how you can hide the price and add to cart button for logged out users. This will encourage visitors to create accounts in your store. With all the user data, you will be able to make well-informed decisions for your online store. However, if you are not familiar with code, I recommend hiring a qualified WordPress developer.
Similar Articles
- How to Hide WooCommerce Update Notifications
- How to Change Button Color Storefront Theme
- How to Hide Shop In WooCommerce
- How to Hide Order Status In WooCommerce
- How to Replace Add to Cart Button With Link WooCommerce
- How to Change Add to Cart Text WooCommerce Guide with Examples
- How to Hide Prices From Google In WooCommerce
- How to Hide Weight of Product in WooCommerce
- How to Add WooCommerce Storefront Sitemap
- How to Hide Quantity In Stock In WooCommerce
- How to Add WooCommerce Storefront Hero Image
- How to Create WooCommerce Secret Product
- How to Hide Errors From Visitors WooCommerce
- How to Hide WooCommerce Registration Form
- How to Add WooCommerce Storefront Product Pagination
- How to Simplify Checkout Free Items Storefront Theme
- How to Hide Shipping Methods for Certain Conditions
- How to Hide Price When Out of Stock In WooCommerce
- How to Hide Price If Zero In WooCommerce
- How to Hide Product Description Heading WooCommerce