Creating a more secure and private shopping experience in your WooCommerce store can be as easy as restricting access to registered users only. In this tutorial, you’ll learn how to automatically redirect visitors who aren’t logged in to your login page — step by step.
🔒 Using a Simple WordPress Code Snippet
To make your WooCommerce store private, all you need is a small piece of code.
You can add it using a code snippets plugin (recommended for beginners) or place it directly inside your theme’s functions.php file if you’re comfortable editing code.
Here’s the snippet:
function my_redirect_non_logged_in_users() {
if ( !is_user_logged_in() && ( is_woocommerce() || is_cart() || is_checkout() ) ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect_non_logged_in_users' );
🧠 How This Code Works
- The function checks if a visitor is not logged in.
- If the user tries to access shop, cart, or checkout pages without logging in, they are instantly redirected to the My Account (login) page.
- It runs automatically in the background, keeping your store’s product and checkout pages visible only to authenticated users.
This ensures that only registered customers can browse and make purchases — adding a simple but effective layer of security.
🧪 Test the Setup
To confirm it’s working correctly:
- Log out of your WordPress admin account.
- Try visiting your shop, cart, or checkout pages.
- You should be redirected to the login page.
- Once logged in, you’ll regain access to all store pages as normal.
✅ Final Thoughts
By requiring users to log in before they can view products or shop, you:
- Protect your WooCommerce store from unauthorized access.
- Create a more exclusive shopping experience for your customers.
- Strengthen your website’s overall security — with minimal effort.
This simple redirect method is one of the easiest ways to keep your WooCommerce store private, safe, and professional.
Tags: #WooCommerceSecurity #PrivateStore #WordPressTips #LoginRedirect