I want customers to be redirected to login if the open the checkout page without being logged in. I found this solution on stackoverflow https://stackoverflow.com/a/50003749/14685541.
So I created a page : Login to checkout
where I put the [woocomerce_my_account] shortcode.
And then added this code to functions.php
function custom_woocommerce_login_redirect_to_checkout_page() {
// Caso 1: Usuario no identificado intenta acceder a Finalizar Compra
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_page_by_path(‘login-to-checkout’) ) );
// Caso 2: Si en la página de identificarse, el cliente ha accedido o se ha registrado, o tiene el carrito vacío, continuar a Finalizar Compra.
if ( is_page(‘login-to-checkout’) ) {
if( is_user_logged_in() || WC()->cart->is_empty() ) {
wp_redirect( get_permalink( get_page_by_path(‘checkout’) ) );
}
}
}
add_action( ‘template_redirect’, ‘custom_woocommerce_login_redirect_to_checkout_page’ );
The first part worked fine, if the user was not logged in and pressed checkout then it is redirecting to the custom login page I had created (https://siristore.in/login-to-checkout)
But,
Once logged it is not redirecting to checkout. Instead it redirects to my account page.
I think due to some custom php code written by xtemos this function is not happening.
Could you please help how to figure this out?
Thank you