How to Hide Prices from Guest Users in WooCommerce
Sometimes, store owners prefer to hide prices on WooCommerce from guest users to encourage them to register or log in. WooCommerce does not have this functionality by default, but you can achieve it with a small custom code snippet.
In this tutorial, we’ll show you how to hide prices from guest users and display custom messages instead.
Add the following code to your theme’s functions.php file:
add_filter( 'woocommerce_get_price_html', 'hide_price_for_guest_users', 10, 2 );
function hide_price_for_guest_users( $price, $product ) {
if ( ! is_user_logged_in() ) {
return '<span class="guest-message">' . __( 'Log in to view price', 'woocommerce' ) . '</span>';
}
return $price;
}
function hide_price_for_guest_users( $price, $product ) {
if ( ! is_user_logged_in() ) {
return '<span class="guest-message">' . __( 'Log in to view price', 'woocommerce' ) . '</span>';
}
return $price;
}
How It Works
- The woocommerce_get_price_html filter modifies the price HTML output for WooCommerce products.
- The is_user_logged_in() function checks whether the user is logged in or not.