How to make the WooCommerce store available only to logged-in users 

In this article, we will explore how to make a WooCommerce store available only to logged-in users and redirect all non-logged-in users to the login page.

There are several benefits of requiring users to log in before accessing a WooCommerce store and redirecting non-logged-in users to the login page, including improved security, a personalized shopping experience, and better WooCommerce customer analytics. By creating a members-only store, businesses can restrict content and provide exclusive access to paying customers and incentivize users to create an account, leading to increased conversions and customer loyalty.

woocommerce restrict access to logged in users

Benefits of making the WooCommerce store available only to logged-in users

Making a WooCommerce store available only to logged-in users and redirecting non-logged-in users to the WooCommerce login page can have several use cases and benefits:

  1. WooCommerce private store: A WooCommerce private store is a shop where products or the entire store are hidden from the public and only accessible to specific users or members. This setup is often used for wholesale customers, membership-based sites, or businesses that want to restrict access to certain products or categories or simply sell access to pages.
  2. Membership or subscription-based website: If you run a membership or subscription-based website, making the WooCommerce store available only to logged-in users can ensure that only paying members have access to the private store.
  3. Personalized Shopping Experience: By making the store available only to logged-in users, you can offer a more personalized shopping experience. You can show users their order history, recommend products based on their previous purchases, and offer discounts to loyal customers.
  4. Improved Security: By requiring users to log in before accessing the store, you can improve the security of your website. You can prevent unauthorized access to your store and protect sensitive customer information.
  5. Better Analytics: Last but not least, requiring users to log in can give you better analytics.By requiring users to log in, you can track their behavior on your website more accurately. You can see which products are popular among your registered users, which pages they visit, and how long they stay on your site. This can help you optimize your website and improve your marketing strategy.

So now that you have some pretty compelling reasons to make your WooCommerce store available only to logged-in users. let’s see how we actually do it.

Adding the custom WordPress function

To make your WooCommerce store only available to logged-in users and redirect non-logged-in users to the login page, you can use a custom WordPress function. Here are the steps to create this functionality with just a few lines of code. To add the custom code you will need a dedicated code snippets plugin or you can add the code to your theme’s functions.php file.

Once you have a way of adding the custom code, you can add the custom WordPress function to the file. To do this, copy and paste the following code at the bottom of the file:

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' );

Understanding the code logic

The function uses the is_user_logged_in() function to check if the user is logged in or not. If the user is not logged in, the function checks if the user is on a WooCommerce store, cart, or checkout page using the is_woocommerce(), is_cart(), and is_checkout() functions.

If the user is not logged in and is on a WooCommerce store, cart, or checkout page, the function uses the wp_redirect() function to redirect the user to the “My Account” page of WooCommerce. Finally, the exit() function is used to terminate the current script to prevent further execution.

The function is hooked to the template_redirect action, which is fired just before WordPress renders the template file. This ensures that non-logged-in users are redirected before they see any content of the WooCommerce store, cart, or checkout page.

Testing the feature

To test the feature, log out of your WordPress account and try to access the WooCommerce store, cart, or checkout page. You should be redirected to the login page forcing you to login in order to access these pages. Once you have logged in, you should be able to access the store as usual.

How to make WooCommerce private store without custom code

There are a few ways to make a WooCommerce store available only to logged-in users without using custom code.

woocommerce membership plugin

Here are two methods you can try:

  1. WooCommerce Memberships: You can use the WooCommerce Memberships plugin to restrict access to your store only to registered users. This plugin allows you to create membership plans that can be purchased by users, and once a user has a membership plan, they will have access to your store. You can also restrict specific products or categories to certain membership plans only.
  2. Password Protected Categories: You can use the password-protected categories plugin to password-protect your product categories. This method is useful if you want to restrict access to specific product categories, rather than the entire store. You can restrict user access and grant access by providing the password to the users in WooCommerce. Once a user enters the correct password, they will have access to the password-protected category.
  3. WooCommerce for Logged-in Users plugin: This plugin allows you to control who can access your store, and it ensures that non-logged-in users are redirected to the login page.

All of these methods provide an easy way to restrict access to your WooCommerce store without custom code. Some of them are free and some of the plugins are premium. However, note that they do require the installation of additional plugins, which can affect your website’s performance.

Using  the WooCommerce for Logged-in Users plugin

To restrict access to your WooCommerce store only to logged-in users, you can use the WooCommerce for Logged-in Users WordPress plugin. This plugin is free, easy to use and requires no coding skills to set up. Once you have activated the plugin, it will automatically redirect non-logged-in users to the “My Account” page where they can log in to access the store.

woocommerce login only plugin

To get started, you’ll need to install and activate the plugin. Here’s how to do it:

Go to the Plugins page in your WordPress dashboard and click “Add New”. In the search field, type “WooCommerce for Logged-in Users” and hit enter. Click “Install Now” and then “Activate”.

Tracking WooCommerce customer logins with Last Seen report

If you’re using the Activity module in WooCommerce, there’s now a “Last Seen” report available.

WordPress user last login report

WooCommerce new vs returning customers report

This report tracks user activity by showing how many WooCommerce customers have logged in during specific periods, such as Today, the Last 7 days, and the Last 30 days. This feature helps you monitor engagement and ensure that your store is available exclusively to logged-in users, enhancing control over access and user behavior insights.

That’s it! Now, only logged-in users will be able to access your WooCommerce store.

The plugin will redirect non-logged-in users to the WooCommerce “my account” page as specified in the plugin settings. With this simple plugin, you can ensure that your store and WooCommerce products are available only to the logged in users who have an account on your website, and keep your store secure from unauthorized access.

Unlock the Full Potential of Your WooCommerce Customer Data

Conclusion

By implementing the strategy of limiting access to your WooCommerce store to logged-in users exclusively, you can fortify your customers’ data security and uphold your store’s overall protection. This uncomplicated yet effective approach can be executed by following the steps outlined in this tutorial, which will facilitate converting your WooCommerce store to be accessible solely to registered users and rerouting non-logged-in users to the login page.