How to Hide Product Prices Based on User Role Storefront

Hiding Product Prices Based on User Role in WooCommerce StorefrontPricing is very important in any eCommerce store. This is because prices are the focus of your research, product comparisons, and trend analyses. You may want to hide the product prices based on certain user roles in your WooCommerce store as a marketing strategy.

However, WooCommerce, by default, does not have an option to hide prices. This post focuses on pricing based on the user role, or lack thereof (guest users).

Storefront Hide Product Prices Based on User Role

The main aim of doing this is to encourage users to sign up to see the prices. If you want to add this functionality, stick to the end. The best part is I’ll give you a step by step guide on how to hide the prices. However, you need to have some technical knowledge.

Why Hide Price Until Login?

Many stores may not want everyone to browse their online store and view their product catalog. Here are some examples of stores that typically hide price:

  • Wholesale stores that do not sell directly to the general public
  • Manufacturers that publish details of their products but allow approved resellers to purchase from them.
  • WooCommerce members-only stores

Steps to Hide Prices for Guest Users

The first thing you need to do is hide prices for all users that are not logged in. Here are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. 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 guest users’ prices.
  3. Add the following code to the functions.php file of the Storefront theme.
/**

 * Hide product price based on user role.

 */

function njengah_hide_prices_guests( $price ) {

            if ( ! is_user_logged_in() ) {

                        return ''; // Return a empty string for no price display.
            }

            return $price;
}
add_filter( 'woocommerce_get_price_html', 'njengah_hide_prices_guests' ); // Hide product price
  1. This is the outcome:hide products from guests

Steps to Hide the Cart and Checkout Prices and Totals for Guest Users

The next step is to hide the cart and checkout prices and totals. Add the following lines in the functions.php file. Here are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. 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 the cart and checkout prices and totals for guest users.
  3. Add the following code to the functions.php file of the Storefront theme.
// Cart

add_filter( 'woocommerce_cart_item_price', 'njengah_hide_prices_guests' ); // Hide cart item price

add_filter( 'woocommerce_cart_item_subtotal', 'njengah_hide_prices_guests' ); // Hide cart total price

hide prices from the cart

  1. To remove the ‘Price’ and ‘Total’ table headings, you can use a CSS snippet to hide it. This PHP snippet will only add the CSS when the user is not logged in:
/**

* Hide price/total table headings with CSS.

*/

function njengah_hide_cart_checkout_price_headings_guests() {

if ( ! is_user_logged_in() ) {

?><style>

.product-price, .product-subtotal, /* Cart */

.woocommerce-mini-cart__total, /* Cart widget */

.product-total, .cart-subtotal, .order-total /* Checkout */

{ display: none !important; }

</style><?php

}

}

add_action( 'wp_head', 'njengah_hide_cart_checkout_price_headings_guests' );
  1. This is the outcome:remove price and total

If your store has a wholesale user role, you can also hide prices for regular or guest users. The following code snippet will only show prices for wholesale customers. It should be added in the functions.php file:

/**

* Hide product price based on user role (or lack thereof).

*/

function njengah_hide_prices_user_role( $price ) {

$current_user = wp_get_current_user();

$allowed_roles = array( 'wholesale', 'administrator' );

if ( ! array_intersect( $current_user->roles, $allowed_roles ) ) {

return '';

}

return $price;

}

add_filter( 'woocommerce_get_price_html', 'njengah_hide_prices_user_role' ); // Hide product price

// Cart

add_filter( 'woocommerce_cart_item_price', 'njengah_hide_prices_user_role' ); // Hide cart item price

add_filter( 'woocommerce_cart_item_subtotal', 'njengah_hide_prices_user_role' ); // Hide cart total price

// Checkout totals

add_filter( 'woocommerce_cart_subtotal', 'njengah_hide_prices_user_role' ); // Hide cart subtotal price

add_filter( 'woocommerce_cart_total', 'njengah_hide_prices_user_role' ); // Hide cart total price

/**

* Hide price/total table headings with CSS.

*/

function njengah_hide_cart_checkout_price_headings() {

$current_user = wp_get_current_user();

$allowed_roles = array( 'wholesale', 'administrator' );

if ( ! array_intersect( $current_user->roles, $allowed_roles ) ) {

?><style>

.product-price, .product-subtotal, /* Cart */

.woocommerce-mini-cart__total, /* Cart widget */

.product-total, .cart-subtotal, .order-total /* Checkout */

{ display: none !important; }

</style><?php

}

}

add_action( 'wp_head', 'njengah_hide_cart_checkout_price_headings' );

Conclusion

In summary, I have shared how you can hide prices and show them to wholesalers. However, you can modify the ‘wholesale’ user role to your user role slug you would like to show product prices. Additionally, I have also added the ‘administrator’ role, so admins also see the prices. Moreover, you will know how to add additional user roles that will see product prices.

Similar Articles