WooCommerce Force User Redirect to Login Page
Today’s discussion is about how to force the user to redirect to the login page programmatically in WooCommerce. This functionality is usually required when the shop owner decides to limit his website for the purchase to be done by private users or login users only.
Following is an example
add_action( 'init', 'qaisarsatti_store_redirect' );
function qaisarsatti_store_redirect() {
if ( !is_user_logged_in() ) {
$loginUrl = wp_login_url();
$currentUrl = home_url($_SERVER['REQUEST_URI']);
if($loginUrl!=$currentUrl) {
wp_redirect( wp_login_url());
exit;
}
}
}
function qaisarsatti_store_redirect() {
if ( !is_user_logged_in() ) {
$loginUrl = wp_login_url();
$currentUrl = home_url($_SERVER['REQUEST_URI']);
if($loginUrl!=$currentUrl) {
wp_redirect( wp_login_url());
exit;
}
}
}
Here is an available free WooCommerce B2B Customer extension.