Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm developing my own theme Woocommerce , and now I need to do a redirect after logging in a particular page where I am using Woocommerce login form. To this I added the following function in functions.php

if (!is_account_page()) {
add_filter('woocommerce_login_redirect', 'redirect_after_login_cart');
   function redirect_after_login_cart(){
       wp_redirect( get_permalink( get_page_by_path(‘checkout’) ) );
       exit;
   }
}

I want only the login-cart page, do the redirect after login to the checkout page. The form of the my-account is still redirecting to the my-account page. The way it is , all my Woocommerce login forms are redirecting to the checkout page. ?The conditional is not being respected.

Before going to the checkout page, I check if you are logged in or not. If you are logged in, redirect to checkout, but redirects to a page created with the login form. When logging on that page, instead of going to my account page, redirects to the checkout.

The default Woocommerce is every time you log in, go to my account, and I'm changing this form only if you are on page I created login-cart

add_action('template_redirect','check_if_logged_in');
function check_if_logged_in(){
   if(!is_user_logged_in() && is_checkout()){
    wp_redirect( get_permalink( get_page_by_path('login-cart') ) );
    exit;
   }
}

If anyone can help me , I will be very grateful .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

I managed to solve my problem. Follow the code below:

add_action( 'woocommerce_login_form', 'redirect_user_to_checkout' );
function redirect_user_to_checkout() {
    $referer = get_permalink( get_page_by_path('finalizar-compra'));
if( $referer ) {
    if(!is_account_page()) { ?>
            <input type="hidden" name="redirect-user" value="<?php echo $referer; ?>"><?php
        }
    }
}



function custom_woocommerce_login_redirect_to_checkout_page( $redirect, $user ) {
    if( isset( $_POST['redirect-user'] ) ) {
        $redirect = esc_url( $_POST['redirect-user'] );
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'custom_woocommerce_login_redirect_to_checkout_page' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...