Skip to main content

Force Visitors to Login Before Accessing WooCommerce Shop

Topic WordPress, Security
Access FTP

The Simplest Way to Prevent Random Visitors From Accessing Your Store Without Using a Plugin

Sometimes WooCommerce shop owners want to restrict non-logged in users from accessing the shop. There are a number of reasons this might be useful, such as a shop geared towards wholesaling or a shop selling specific products or services to a group of clients or customers. For example, Stackpilot provides some development services to agencies and has turned WooCommerce into a wholesale pricing catalog for everything from WordPress websites to configuring 3rd party applications like Zendesk or Intercom for their clients. This system is separate from our hosting billing system because it’s geared towards agencies who are ultimately invoiced for the work and pay lower than retail rates for the services.

To prevent visitors who aren’t logged in from browsing your shop you’ll want simply add the following code to your functions.php file. In doing so, any logged out user attempting to access any WooCommerce page will be re-directed to my-account which, when logged out, will prompt the user to login. Once they login they’re free to use your shop!

function woo_login_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
wp_redirect( '/my-account/' );
add_action('template_redirect', 'woo_login_redirect');

This is pretty straightforward easy to implement. As always, if you’re using a commercial theme make sure you’ve setup a proper child theme and add this to the functions.php file in the child theme folder to prevent accidentally overwriting this during a theme update!


  • aaron says:

    Hi, in the code above, do you mean to have the “&&” or should that just be “&&” for the code?

    • Craig Marolf says:

      Hey @aaron — Looks like we were having some issues with our code plugin and it rewrote the && to & – Thanks to you we went out and found a better plugin. We’re now using “Code Snippets CPT” which seems to work really well.

      Actually, your comment is what prompted us to make some changes here, so thanks for calling it to our attention!

      FYI, I wonder if you might be having a similar issue on your site, I noticed it on your recent blog post “Website JSON-LD Schema For Multiple Locations” — Give Code Snippets CPT a look, so far we’re happy.

  • Hello,
    Thanks for writing post.
    I want to user to be login in to view product details. Is there any plugin / code snippet.
    Let me know.


    • Craig Marolf says:

      Hey @Somnath! this snippet will force the user to login to view the entire product. If you’d like to make users login to just see the actual product details on the product page but not prevent them from seeing the actual product, you could try using an if else for is_user_logged_in()

      For example, if you wanted to hide the product description for logged out users you could use something like this:

      There might be more efficient ways to write this, to include writing a function in your theme’s functions.php file (recommended when possible) depending on what you’re trying to accomplish, but that’s the gist of it.

      If you opened /wp-content/plugins/woocommerce/single-product/tabs/description.php you could use this code there to hide the description for logged-out users.

      Make sure to never modify WooCommerce files directly- you’ll want to add any files you customize to your theme (or child-theme when applicable).

      You could also try to find a plugin that’ll suit your needs.

Leave a Reply

Quick Open Ticket

You can quickly open a ticket without logging in. Be sure to use your login email address and include the web address of your service.