How to Hide Prices from Guest Users in WooCommerce

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:

[cc lang="php" tab_size="2" lines="40"]
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 '' . __( 'Log in to view price', 'woocommerce' ) . '';
    }
    return $price;
}
[/cc]

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.

haris raza

Hi, I'm Haris Raza! With years of experience as a WordPress backend developer, I specialize in custom PHP development, WooCommerce plugin creation, and building custom themes from scratch. Whether it's developing WordPress plugins or customizing existing ones, my goal is to provide seamless and efficient solutions that meet your unique needs. When I'm not coding, you can find me keeping up with the latest tech trends and collaborating with fellow developers. Thanks for visiting, and happy coding http://www.wpharis.com