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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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'); |
Source: User must be logged in to view WooCommerce Products | Web Design 101