[wd_asp elements=’search’ ratio=’100%’ id=1]

User must be logged in to view WooCommerce Products without plugin!

13th September 2018

WooCommerce

woocommerce category codehaven

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

Source: User must be logged in to view WooCommerce Products | Web Design 101