| |

How to Make WooCommerce Store Exclusive to Logged-In Users

Creating a secure shopping experience on your WooCommerce store can be as simple as ensuring only registered users can view your products and shop. This guide will show you, in easy steps, how to direct anyone who isn’t logged in to the login page.

Using a Simple WordPress Code

To make your store private, you’ll need to add a small piece of code to your website. You can do this using a tool that helps you add custom code (like a code snippets plugin) or you can put the code in a special file called functions.php if you feel comfortable doing so. Here’s the code you need:

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 the Code Works

This code checks if a person visiting your store is logged in or not. If they aren’t logged in and are trying to visit the product pages, cart, or checkout, the code sends them straight to the login page. This helps protect your store from unwanted visitors.

The code works in the background every time someone visits, making sure only logged-in users can see your products and other shopping pages.

Testing to Make Sure It Works

To check if everything is set up correctly, log out of your WordPress account and try visiting your store, cart, or checkout pages. If everything is working, you should be taken to the login page. Once you log in, you should be able to shop as normal.

By making sure only logged-in users can access your WooCommerce store, you create a safer and more exclusive shopping experience for your customers. It’s a straightforward way to boost your site’s security effortlessly.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *