In a earlier post I wrote about hiding product prices on category pages. This post is similar on hiding prices, but focuses on the hiding of pricing based on the user role, or lack thereof (guest users).

Hiding Prices for Guest Users

First thing I want to show is hiding prices for all users that are not logged in. The snippet below will hide all product prices on the overview and product detail pages. 

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Hide product price based on user role.
*/
function ace_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', 'ace_hide_prices_guests' ); // Hide product price

Hiding Cart/Checkout Prices and Totals for Guest Users

The above snippet doesn’t hide the prices or totals in the cart/checkout, to do that you’d want to add these additional lines of code to the prior snippet;

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
// Cart
add_filter( 'woocommerce_cart_item_price', 'ace_hide_prices_guests' ); // Hide cart item price
add_filter( 'woocommerce_cart_item_subtotal', 'ace_hide_prices_guests' ); // Hide cart total price

This snippet will hide the product prices in the items and total tables; however it will not remove the ‘Price’ and ‘Total’ table headings. If you also want to remove that, a snippet of CSS will can to be used to hide it. This is a PHP snippet that will only add the CSS when the user is not logged in.

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Hide price/total table headings with CSS.
*/
function ace_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', 'ace_hide_cart_checkout_price_headings_guests' );

Hiding Prices for Customers, Showing for Wholesale User Role

The prior snippets were only hiding prices for guest users. Next up are the same snippets with some changes. These (combined) snippet will only show prices for wholesale customers, not for regular or guest users.

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Hide product price based on user role (or lack thereof).
*/
function ace_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', 'ace_hide_prices_user_role' ); // Hide product price
// Cart
add_filter( 'woocommerce_cart_item_price', 'ace_hide_prices_user_role' ); // Hide cart item price
add_filter( 'woocommerce_cart_item_subtotal', 'ace_hide_prices_user_role' ); // Hide cart total price
// Checkout totals
add_filter( 'woocommerce_cart_subtotal', 'ace_hide_prices_user_role' ); // Hide cart subtotal price
add_filter( 'woocommerce_cart_total', 'ace_hide_prices_user_role' ); // Hide cart total price
/**
* Hide price/total table headings with CSS.
*/
function ace_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', 'ace_hide_cart_checkout_price_headings' );

In these snippets you can of course modify the ‘wholesale’ user role to your user role slug you’d like to show product prices for. In the snippet I’ve also added the ‘administrator’ role so admins also see the prices, plus you’ll know how to add additional user roles that will be able to see product prices.

About the author: Jeroen Sormani is actively building WordPress, WooCommerce and Easy Digital Downloads plugins. Slightly obsessed by writing high quality code.
Follow Jeroen on Twitter

2 comments

    1. Hi Vanessa,

      That is possible of course; you’d want to further customize the snippet to include the subcategories (by default it only takes the direct matching categories).
      You can change the snippet to show only for the setup user roles (in this example its only set for ‘administrator’ and ‘wholesale’ roles.

Leave a Reply

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