Woocommerce hide price for logged out users without plugin

Last edited:
October 31, 2023
Reading time:
5 mins
Topics:
B2B, Code Snippet

/

Blog

/

WooCommerce

/

Woocommerce hide price fo...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

In the world of e-commerce, personalization and user experience are paramount. One way to enhance user experience and potentially increase sales is by hiding product prices for logged-out users. While this approach may seem counterintuitive at first, there are specific scenarios where it makes sense. In this article, we’ll explore the reasons why you might want to hide prices for certain users and provide step-by-step instructions on how to achieve this without the need for a plugin.

Why Hide WooCommerce Prices for logged out users?

  1. Encouraging User Registration: Hiding prices for non-registered or logged-out users can be a strong incentive for them to create an account on your WooCommerce website. This strategy is often used to build a loyal customer base and gather valuable customer data.
  2. Wholesale or B2B Scenarios: For businesses dealing with wholesale customers or in a B2B (business-to-business) setting, you may prefer to negotiate prices individually with each client. In such cases, showing a “Request a Quote” button instead of prices can be more appropriate.
  3. Exclusive Membership Pricing: If your store offers exclusive membership or loyalty programs, you can provide discounts or special pricing to registered members while keeping prices hidden from the general public.
  4. Price Customization: Some businesses might need to offer customized pricing based on customer segments, making it necessary to hide standard prices from non-targeted users.

Hide prices with a code snippet in functions.php

Here are step-by-step instructions to hide product prices for logged-out users in WooCommerce without using a plugin:

1. Child Theme Setup (If Not Already Done)

Before making any code changes, ensure that you are using a child theme to avoid potential issues during theme updates. If you’re not using a child theme, set one up first.

2. Access functions.php

You can modify the functions.php file through various methods:

a. WordPress Theme Editor:

  • Log in to your WordPress admin dashboard.
  • Go to “Appearance” and select “Theme Editor.”
  • On the right-hand side, you will see a list of theme files. Click on functions.php to edit the file.

b. cPanel or Hosting Panel:

  • Log in to your hosting panel (cPanel, Plesk, etc.).
  • Navigate to the “File Manager” or “Files” section.
  • Find the root directory of your WordPress website (usually it’s the “public_html” folder).
  • Inside the root directory, locate your child theme folder (typically named after your theme with a “-child“, e.g. mytheme-child).
  • In the theme folder, you will find the functions.php file. Right-click on it and select “Edit” or “Code Edit.”

c. FTP or SSH:

  • Connect to your website via FTP (File Transfer Protocol) or SSH (Secure Shell).
  • Locate the WordPress root directory on your server.
  • Navigate to the “wp-content/themes/your-child-theme” directory.
  • Edit the functions.php file either online or by downloading it to your local machine (and reuploading it).

Whichever method you choose, make sure to backup your functions.php file before making any changes to avoid accidental errors.

3. Add Code to Hide Prices

To hide prices for logged-out users, you’ll need to insert the following code into your functions.php file:

add_filter('woocommerce_get_price_html', 'hide_prices_for_guests');
add_filter('woocommerce_cart_item_price', 'hide_prices_for_guests');
add_filter('woocommerce_cart_item_subtotal', 'hide_prices_for_guests');

function hide_prices_for_guests($price) {

   if (!is_user_logged_in()) {

       $login_url = get_permalink(get_option('woocommerce_myaccount_page_id')); // Get the My Account page URL

       $login_message = 'Please login to view prices.'; // Customize this message as needed 

       echo '<a href="'.$login_url.'">Log-in/Register</a>'; 

    return $login_message; 
   } 
   return $price; 
}

This code filters the product prices in various parts of your WooCommerce store and replaces them with the “Price on request” message for logged-out users. You can customize the message to fit your specific needs. For example, we added a link to My Account page to make our visitors lives easier in case they are not logged in or registered.

Testing

Test your website by logging out and visiting the product pages to ensure that the prices are hidden for logged-out users. Make sure you visit your website with cleared cache and without forgetting to log out. Preferably, open a different browser or an additional incognito window and make your tests through there.

Customization

You can further customize the code to meet your requirements. For example, you can show different messages based on your business needs, or even redirect logged-out users to a login or registration page when they attempt to view prices.

Hide your WooCommerce store prices for unregistered visitors

Hiding product prices for logged-out users in WooCommerce can be a strategic move in various scenarios. Whether you want to encourage user registration, cater to B2B clients, or offer exclusive membership pricing, this approach can enhance your e-commerce strategy. With the provided instructions, you can implement this feature without relying on additional plugins, giving you more control over your WooCommerce store.

🚀 Suggested Reading: Hiding price for logged-out visitors is now seamless. But what about tracking those visitors no matter if they are logged-out or logged-in? Take a look into our article about how to install Google Tag Manager to WooCommerce and setup your first events.