How to Hiding product prices in WooCommerce

There are many plugins that allow you to setup special accounts for your wholesalers for example. Often times the user role feature of WordPress is used to identify users within a specific group. In this post I’ll show some examples of how you can display/hide prices for specific user groups, including only for specific products / categories.

The snippets in this post will only be for hiding prices, it will still continue users to add products to the cart.

Hiding all the prices

Lets start with a simple script first. This is a example of how you can hide all the prices no matter who is viewing.

add_filter( ‘woocommerce_get_price_html’, function( $price ) {
if ( is_admin() ) return $price;

return ”;
} );

I’ve made it so that it does still show prices on the admin, but you can easily comment that line out if you also want to hide prices there.

This code snippet hides all the prices on the product / archive pages, but not on the cart/checkout pages (or the cart widget). The following code also removes the cart item price / subtotal and the same for the checkout.

<?php
// Copy from below here
add_filter( ‘woocommerce_get_price_html’, function( $price ) {
if ( is_admin() ) return $price;
return ”;
} );
add_filter( ‘woocommerce_cart_item_price’, ‘__return_false’ );
add_filter( ‘woocommerce_cart_item_subtotal’, ‘__return_false’ );

After using this code snippet it will leave the headings in the table. Unfortunately these cannot be removed with a code snippet, but there are two alternatives.

  1. By modifying the WooCommerce template files you can remove the columns from the totals table. The totals table is located in the cart/cart.php template file.
  2. You can hide them with CSS. A technical user could still uncover the product prices through, so depending on how important it is, this may or may not fit your requirements.

Here’s a CSS snippet that hides the prices on the items on the cart/checkout table and the cart widget:

/* Cart widget */
.woocommerce-mini-cart__total {
display: none;
}

/* Cart */
.product-price, .product-subtotal {
display: none !important;
}

/* Checkout */
.product-total *, th.product-total {
display: none;
}

Again, this is not a foolproof solution.

Hiding prices for specific user roles

In the below code snippet it will hide the prices for all users with the ‘wholesale’ user role. In the sample code I’ve added multiple rows to show how you can can hide prices for different user roles. Adding more options to it is possible by following the shown format.

<?php
// Copy from below here
add_filter( ‘woocommerce_get_price_html’, function( $price ) {
if ( is_admin() ) return $price;
$user = wp_get_current_user();
$hide_for_roles = array( ‘wholesale’, ‘wholesale-silver’, ‘wholesale-gold’ );
// If one of the user roles is in the list of roles to hide for.
if ( array_intersect( $user->roles, $hide_for_roles ) ) {
return ”; // Return empty string to hide.
}
return $price; // Return original price
} );
add_filter( ‘woocommerce_cart_item_price’, ‘__return_false’ );
add_filter( ‘woocommerce_cart_item_subtotal’, ‘__return_false’ );

Hiding prices in specific categories

With this version of the code snippet you can target specific categories to hide the prices for.

<?php
// Copy from below here
add_filter( ‘woocommerce_get_price_html’, function( $price, $product ) {
if ( is_admin() ) return $price;
// Hide for these category slugs / IDs
$hide_for_categories = array( ‘singles’, ‘albums’ );
// Don’t show price when its in one of the categories
if ( has_term( $hide_for_categories, ‘product_cat’, $product->get_id() ) ) {
return ”;
}
return $price; // Return original price
}, 10, 2 );
add_filter( ‘woocommerce_cart_item_price’, ‘__return_false’ );
add_filter( ‘woocommerce_cart_item_subtotal’, ‘__return_false’ );

You can either modify the set categories to the slug of ID of the category. If wanted you can also combine the snippets to only hide the prices in specific categories for a guest user;

if ( ! is_user_logged_in() ) {

$hide_for_categories = array( ‘singles’, ‘albums’ );
if ( has_term( $hide_for_categories, ‘product_cat’, $product->get_id() ) ) {
return ”;
}
}

Hiding prices for specific products

Here’s one last version where it only hides the prices for specific products based on the product ID.

<?php
// Copy from below here
add_filter( ‘woocommerce_get_price_html’, function( $price, $product ) {
$hide_for_products = array( 95, 115 );
if ( in_array( $product->get_id(), $hide_for_products ) ) {
return ”;
}
return $price; // Return original price
}, 10, 2 );
add_filter( ‘woocommerce_cart_item_price’, ‘__return_false’ );
add_filter( ‘woocommerce_cart_item_subtotal’, ‘__return_false’ );

Hope you’ve found this post helpful!