Pricing 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:
- 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 guest users’ prices.
- 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
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:
- 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 the cart and checkout prices and totals for guest users.
- 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
- 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' );
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
- How to Install WooCommerce Storefront Step By Step [Complete Guide]
- How to Set Up Homepage in WooCommerce Storefront
- How to Put WooCommerce Cart and Checkout on One Page
- How to Change Products per Page WooCommerce Storefront Theme
- WooCommerce Storefront Theme Review
- How to Change Storefront Number of Products Per Row
- How to Remove Cart Page WooCommerce Storefront Theme
- How to Remove, Rename and Add Storefront Sorting Options
- How to Change Storefront Theme Order of Products
- How to Hide Tags In Storefront Theme WooCommerce
- How to Remove Related Products WooCommerce Storefront Theme
- How to Turn off Next Product Tabs In WooCommerce Storefront
- How to Hide Mobile Footer From Storefront WooCommerce
- How to Customize Storefront Checkout Page
- How to Change Storefront Theme Product Page Tabs Colors
- How to Remove or Rename Sale Badge WooCommerce Storefront