My desired sequence would be:
User Checkout > Check login >
Proceed to checkout if logged in.
Redirect to login/register page if not logged in > proceed to the checkout page.
I have added below code in my active theme’s function.php file but still having same problem
add_action( 'template_redirect', 'check_if_logged_in' );
function check_if_logged_in() {
$pageid = 817; // your checkout page id
if ( ! is_user_logged_in() && is_page( $pageid ) ) {
$url = add_query_arg(
'redirect_to',
get_permalink( $pageid ),
site_url( '/my-account/' ) // your my acount url
);
wp_redirect( $url );
exit;
}
}
What I am doing wrong? Thanks in advance everyone.
2
Answers
Try this code:
Tested and works.
Simply use a redirect to my-admin when user is not logged in as follows:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.