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

1 Answer
1

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;
    }
}

Leave a Reply

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