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;
}

 

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.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!