2

I’m building a shop in WooCommerce and wish to restrict access & viewing of the shop to logged in members only.

I don’t mind having links to the shop visible (such as in the nav bar), as clicking the shop link, or trying to directly access a shop item would check if the user is logged in, and if not, then redirected to ‘My Account’ to create an account.

Any there any options available to me via plugins or the functions.php?

Secondly, I found an answer the thread below, which is almost exactly what I want: Make WooCommerce pages accessible for logged in users only

I made the suggested changes, but I got an error. Would the error be that I have to insert information unique to my WP install in the following line:

add_action(‘template_redirect’, ‘wpse_131562_redirect’);
2

I just tested this and it will work. Drop this into your functions.php and whenever a user who isn't logged in tries to access a WooCommerce template such as the shop, product pages, cart, or checkout it will redirect them to the WordPress login page.

add_action( 'template_redirect', 'redirect_users_not_logged_in' );
/**
 * Redirect non logged-in users to login page when they try to access any
 * woocommerce template including the cart or checkout page.
 * 
 * @author      Joe Dooley - Developing Designs
 * @return      void
 *
 */
function redirect_users_not_logged_in() {

    if ( ! is_user_logged_in() && ( is_woocommerce() || is_cart() || is_checkout() ) ) {

        auth_redirect();

        exit;
    }
}
  • Thanks Joe! I inserted that into the bottom of functions.php but when I visit the page I get the same error as in my original post: Fatal error: Call to undefined function add_action() in [snip]/functions.php on line 5175 Any thoughts? – Matt Mar 6 '16 at 7:19
  • What error are you getting. Copy and paste the error or post a screen shot please. I tested that right before I posted so there is no way it shouldn't work. Post the error so we can get this working – Joe Dooley Mar 6 '16 at 7:21
  • Okay your adding the code to the wrong function – Joe Dooley Mar 6 '16 at 7:46
  • Okay your adding the code to the wrong functions.php. Please delete whatever you added to the functions.php in your wp-includes folder. Then paste my original code snippet into the functions.php file in wp-content/themes/your -theme-name/functions.php. Side note... Never edit any file in the wp-includes folder or the wp-admin. – Joe Dooley Mar 6 '16 at 7:49
  • Aaahh thanks mate. As you can tell this isn't my forte. Thanks a billion and I'll try this out and update the thread. – Matt Mar 6 '16 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.