Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: Membership

WooCommerce: Force login to purchase (hide price for guests)

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Force login to purchase (hide price for guests)

WooCommerce: Force Login to Purchase (Hide Price for Guests)

If you want to restrict your WooCommerce store so that only logged-in users can see product prices and make purchases, this tutorial will show you how to do it quickly and effectively. By forcing users to log in before viewing prices or adding products to the cart, you can create a members-only shopping experience or better control customer access.

Quick Fix: Require Login to Purchase in WooCommerce

  1. Add a code snippet to your theme’s functions.php file or a site-specific plugin.
  2. Hide product prices and add-to-cart buttons for guests (non-logged-in users).
  3. Redirect guests to the login page when they try to access the cart or checkout.

Why This Happens

By default, WooCommerce displays product prices and allows purchases to all visitors, regardless of login status. However, some stores want to restrict this functionality to registered users only. WooCommerce does not have a built-in setting for this, so you need to customize it by adding code that hides prices and disables purchasing for guests.

This approach is common for wholesale stores, membership sites, or private catalogs where pricing and purchasing should be exclusive.

Requirements

  • WooCommerce installed and active on your WordPress site.
  • Access to your theme’s functions.php file or the ability to add custom PHP snippets via a plugin like Code Snippets.
  • Basic understanding of WordPress and WooCommerce templates.

Step-by-Step: Force Login to Purchase in WooCommerce

  1. Backup your site before making any code changes.
  2. Open your theme’s functions.php file or a site-specific plugin editor.
  3. Add the following code snippet:
<?php
// Hide prices and add to cart button for guests
function wc_hide_price_add_to_cart_for_guests() {
    if ( ! is_user_logged_in() ) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_filter( 'woocommerce_get_price_html', '__return_empty_string' );
        add_filter( 'woocommerce_cart_item_price', '__return_empty_string' );
        add_filter( 'woocommerce_cart_item_subtotal', '__return_empty_string' );
    }
}
add_action( 'wp', 'wc_hide_price_add_to_cart_for_guests' );

// Redirect guests to login page when trying to access cart or checkout
function wc_redirect_non_logged_users() {
    if ( ! is_user_logged_in() && ( is_cart() || is_checkout() ) ) {
        wp_safe_redirect( wp_login_url( wc_get_page_permalink( is_checkout() ? 'checkout' : 'cart' ) ) );
        exit;
    }
}
add_action( 'template_redirect', 'wc_redirect_non_logged_users' );
?>
  1. Save the file and test your site by logging out and visiting product pages, the cart, and checkout.

Common Pitfalls

  • Child Theme vs Parent Theme: Adding code to a parent theme’s functions.php can be overwritten on updates. Use a child theme or a site-specific plugin.
  • Cache Issues: If you use caching plugins or server caching, clear caches after adding the code to see changes immediately.
  • Login Redirect Loops: Ensure the login redirect URL is correct to avoid redirect loops.
  • Custom Themes or Plugins: Some themes or plugins might override WooCommerce hooks, so test thoroughly.

Works On

This solution works on all standard WooCommerce setups running on:

  • Apache and Nginx web servers
  • LiteSpeed servers
  • Hosting control panels like cPanel and Plesk
  • Any WordPress environment where you can add PHP code snippets

FAQ

Q: Can I customize the login redirect URL?
A: Yes, modify the wp_login_url() function parameter to any URL you want users to land on after login.
Q: Will this hide prices on all product types?
A: Yes, the snippet hides prices and add-to-cart buttons for all product types for guests.
Q: Can I show a custom message instead of hiding prices?
A: Yes, you can replace the price filters with a custom message by hooking into woocommerce_get_price_html and returning your message.
Q: Does this affect logged-in users?
No, logged-in users will see prices and can purchase products normally.
Q: Is there a plugin alternative?
Yes, some plugins offer membership or wholesale features that include login-to-purchase restrictions, but this code snippet is lightweight and free.
…
WooCommerce How‑tos

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com