WooCommerce PHP Intermediate Requires WooCommerce

Hide Prices and Add-to-Cart for Non-Logged-In Users

Replace product prices and add-to-cart buttons with a login prompt for unauthenticated visitors, a common pattern for B2B wholesale and membership-gated WooCommerce stores.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 23 lines
// Hide prices on shop and product pages
add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
    if ( is_user_logged_in() ) return $price;
    return '<a href="' . esc_url( wp_login_url( get_permalink() ) ) . '" class="price-login-link">Log in to see pricing</a>';
}, 10, 2 );

// Remove the add-to-cart button and form
add_filter( 'woocommerce_is_purchasable', function( $purchasable, $product ) {
    if ( ! is_user_logged_in() ) return false;
    return $purchasable;
}, 10, 2 );

// Replace the add-to-cart button text on archives
add_filter( 'woocommerce_product_add_to_cart_text', function( $text, $product ) {
    if ( is_user_logged_in() ) return $text;
    return 'Log In to Purchase';
}, 10, 2 );

// Replace button on single product pages
add_filter( 'woocommerce_product_single_add_to_cart_text', function( $text, $product ) {
    if ( is_user_logged_in() ) return $text;
    return 'Log In to Purchase';
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

B2B wholesale stores, membership-gated catalogues, and trade-only WooCommerce sites commonly require visitors to log in before seeing prices or being able to purchase. This creates a lead capture funnel, visitors browse products, hit the login prompt, and register or log in to access pricing. It also keeps your pricing away from competitors browsing your catalogue without an account.

This snippet implements price and purchase gating cleanly using WooCommerce’s built-in filter system, without removing or hiding product listings.

What the code does

Four filters work together to replace both the price display and the purchase mechanism for unauthenticated visitors.

woocommerce_get_price_html

This filter controls the HTML output of every price displayed on your store, regular prices, sale prices, variable product price ranges, and grouped product prices all pass through it. The snippet replaces the price HTML with an anchor tag linking to the login page. wp_login_url( get_permalink() ) sets the login redirect destination to the current page, so after logging in the user lands back on the product they were viewing.

woocommerce_is_purchasable

Setting this filter to return false for non-logged-in users removes the add-to-cart form from the single product page and disables add-to-cart functionality entirely for those users. WooCommerce won’t render the quantity field or the form when is_purchasable() returns false. This is the cleanest way to gate purchasing, it operates at the data layer rather than hiding elements with CSS, which could be circumvented.

Add-to-Cart Button Text

The two button text filters replace the default “Add to cart” text with “Log In to Purchase” for unauthenticated visitors on both the archive loop and single product pages. Because is_purchasable() is returning false, these buttons won’t actually add anything to the cart, but displaying them with clear instructional text is better UX than showing a broken button or no button at all.

Allowing Guest Access to Specific Products

If some products should be purchasable without login, a free sample, an introductory product, or a lead magnet, add a product meta check to the woocommerce_is_purchasable filter. Store a custom field on those products and check for it before returning false. This lets you gate most products while keeping specific ones publicly purchasable.

Registration Prompt

Pair this snippet with a registration prompt on the login page. If visitors can only see products after creating an account, the login page should make it obvious how to register, and registration should be fast and friction-free. WooCommerce’s My Account page includes registration functionality by default; make sure it’s enabled in WooCommerce → Settings → Accounts & Privacy.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only