I use the following code (pasted in my theme functions.php file) to redirect unlogged users from checkout page to WooCommerce My Account login form: add_action( 'template_redirect', 'redirect_to_specific_page' ); function redirect_to_specific_page() { if ( is_page('finalizar-compra') && ! is_user_logged_in() ) { wp_redirect( 'https:/miweb.pe/login', 301 ); exit; } } How can I redirect users, once they got logged in, back to the previous URL? Here, the previous URL would be: https://miweb.com/finalizar-compra. I've tried using https://miweb.pe/login/?redirect_to=https://miweb.pe/finalizar-compra, but it doesn't work.
Jese Leos
August 10, 2024
Verified user
In WooCommerce checkout, you can directly log in without any redirection, but it requires enabling the option from WooCommerce settings > Accounts and Privacy > Guest checkout: Then in the checkout page you will have something like: Now if you wish making redirections, try the following modular and complete code, to redirect users, once they got logged in, back to the checkout URL from My account: add_action( 'template_redirect', 'redirect_to_specific_page' ); function redirect_to_specific_page() { $is_checkout_page = is_checkout() && !is_wc_endpoint_url(); if ( !is_user_logged_in() && $is_checkout_page ) { global $wp; $redirect_to = '/?redirect_to=' . urlencode( site_url( $wp->request ) ); wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')) . $redirect_to ); exit(); } } Code goes in functions.php file of your child theme (or in a plugin).