How to restrict Non-login visitors in Woocommerce? | themelocation

How to restrict Non-login visitors in Woocommerce?

In this tutorial we’ll learn how to make Woocommerce pages accessible for logged in users only.

By default Woocommerce comes with four pages:

  • Shop Page
  • Cart Page
  • Checkout Page
  • My Account Page

These pages are accessible to all users by default.

Wocommerce

As we can see by default we can access shop and other Woocommerce pages without logging in.

Now to restrict these pages from non-logged in users, add the following lines of code at the end of your theme’s functions.php file.

function wpse_131562_redirect() {

   if (

       ! is_user_logged_in()

&& (is_woocommerce() || is_cart() || is_checkout())

   ) {

       // feel free to customize the following line to suit your needs

       wp_redirect(home_url());

       exit;

   }

}

add_action('template_redirect', 'wpse_131562_redirect');

Now after saving the file if we try to access any of the Woocommerce page it won’t let us until we log in and redirect us to home page.

Even if I refreshed already opened shop page it redirected me to home page.

Wocommerce

No comments yet.

Leave a Reply