Sometimes, you want a good old simple way to restrict customers who don’t have accounts to view your products on your WooCommmerce website. A reason why I’m setting this up for a client is because they have two websites. One of them is for the public, the second is for wholesale customers who need to have accounts before they can access the products. Rather than purchasing another plugin, there’s a fast way to make this work without spending a cent.
Redirect to my account page
Basically, this code tells WooCommerce that if someone is not signed in and visits a WooCommerce page (products, cart, checkout) – then it will redirect them to the my account page to sign up or log in.
function customer_login_redirect() { $dir = get_bloginfo('url'); // change /my-account/ to any page on your website $url = $dir.'/my-account/'; if ( ! is_user_logged_in() && (is_woocommerce() || is_cart() || is_checkout()) ) { // feel free to customize the following line to suit your needs wp_redirect($url); exit; } } add_action('template_redirect', 'customer_login_redirect');
Redirect to home page
This redirects user straight to what ever you’ve set the home/front page to be on your website.
function customer_login_redirect() { if ( ! is_user_logged_in() && (is_woocommerce() || is_cart() || is_checkout()) ) { wp_redirect(home_url()); exit; } } add_action('template_redirect', 'customer_login_redirect');
These have been successfully tested with WooCommerce 2.6.4