Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

php - Login redirection on different pages on my theme Woocommerce

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...