Require Login to Browse the Shop
Sends visitors who are not logged in from the shop, product, cart and checkout pages to the My Account login form, for wholesale and members-only stores.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( is_user_logged_in() || ! function_exists( 'is_woocommerce' ) || is_account_page() ) {
return;
}
if ( is_woocommerce() || is_cart() || is_checkout() ) {
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
exit;
}
} );
add_filter( 'woocommerce_login_redirect', function ( $redirect ) {
$shop = wc_get_page_permalink( 'shop' );
return $shop ? $shop : $redirect;
}, 10 );
Notes
The My Account page stays open so customers can log in or register. After logging in, customers go back to the shop.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.