0

I want to apply following 2 case :

If User not logged in and cart has product: Then redirect user to login and after login redirect to checkout page. I have used a code but it redirecting to the current page i.e if we check out from home page it will redirect to home page itself after login, if it is from shop page it will redirect to shop page. I want to redirect to checkout page only. My code

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
    $pageid = get_option( 'woocommerce_checkout_page_id' );
    if(!is_user_logged_in() && is_page($pageid))
    {
        $url = add_query_arg(
            'redirect_to',
            get_permalink($pagid),
            site_url('/my-account/') // your my account url
        );
        wp_redirect($url);
        exit;
    }
    if(is_user_logged_in())
    {
    if(is_page(get_option( 'woocommerce_myaccount_page_id' )))
    {
        
        $redirect = $_GET['redirect_to'];
        if (isset($redirect)) {
        echo '<script>window.location.href = "'.$redirect.'";</script>';
        }

    }
    }
}

2 Answers 2

0

case 1:

function wpse_131562_redirect() {
    if (
        ! is_user_logged_in()
        && (is_cart() || is_checkout())
    ) {
        // feel free to customize the following line to suit your needs
         wp_redirect( home_url( '/my-account/' ) );
        exit;
    }
}
add_action('template_redirect', 'wpse_131562_redirect');
0

case 2:

add_filter( 'woocommerce_login_redirect', 'jabir_woocommerce_redirect_after_login', 9999, 2 );
 
function jabir_woocommerce_redirect_after_login( $redirect, $user ) {
     
        
        $redirect = wc_get_page_permalink( 'checkout' ); // checkout
        
    
  
    return $redirect;
}

Hope This two hooks help you to get what you want, thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.