WooCommerce - 登录后重定向到上一页 [英] WooCommerce - redirect to previous page after login

查看:33
本文介绍了WooCommerce - 登录后重定向到上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找可以处理数小时的插件和片段,但没有成功.每个答案都不适用于我.我在菜单中有登录"链接,通向 WooCommerce 的我的帐户"页面,其中显示了登录表单.我希望客户在成功登录后返回到点击登录"链接的页面.

I've been searching for plugins and snippets which would handle it for hours, but no success. Every single answer doesn't work for me. I have "Log in" link in menu, leading to WooCommerce "My account" page, which shows login form. I want customers to return to page where "Log in" link was clicked after successful login.

wp_get_referer() 不返回任何内容,并且 $_SERVER["HTTP_REFERER"] 如果放在与 woocommerce_login_redirect (我用 PHP 调试控制台检查).

wp_get_referer() doesn't return anything and $_SERVER["HTTP_REFERER"] returns my account page if put within function hooked to woocommerce_login_redirect (I used PHP debug console to check).

这是我的代码:

// Redirect user after login.
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
    $role = $user->roles[0];
    $dashboard = admin_url();       

    if (in_array($role, array('administrator', 'shop_manager', 'editor', 'author', 'contributor'))) {
        $redirect = $dashboard;         
    } elseif (in_array($role, array('customer', 'subscriber'))) {
        $redirect = $_SERVER["HTTP_REFERER"];          
    } else {
        $redirect = $_SERVER["HTTP_REFERER"];       
    }

    return $redirect;

}

这是我使用的过滤器出现在 WooCommerce 代码中的位置:

Here is where filter which I used appears in WooCommerce code:

/** 
   * Process the login form. 
   */ 
  public static function process_login() { 
      $nonce_value = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : ''; 
      $nonce_value = isset( $_POST['woocommerce-login-nonce'] ) ? $_POST['woocommerce-login-nonce'] : $nonce_value; 

      if ( ! empty( $_POST['login'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-login' ) ) { 

          try { 
              $creds = array( 
                  'user_password' => $_POST['password'],  
                  'remember' => isset( $_POST['rememberme'] ),  
 ); 

              $username = trim( $_POST['username'] ); 
              $validation_error = new WP_Error(); 
              $validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $_POST['username'], $_POST['password'] ); 

              if ( $validation_error->get_error_code() ) { 
                  throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . $validation_error->get_error_message() ); 
              } 

              if ( empty( $username ) ) { 
                  throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Username is required.', 'woocommerce' ) ); 
              } 

              if ( is_email( $username ) && apply_filters( 'woocommerce_get_username_from_email', true ) ) { 
                  $user = get_user_by( 'email', $username ); 

                  if ( isset( $user->user_login ) ) { 
                      $creds['user_login'] = $user->user_login; 
                  } else { 
                      throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'A user could not be found with this email address.', 'woocommerce' ) ); 
                  } 
              } else { 
                  $creds['user_login'] = $username; 
              } 

              // On multisite, ensure user exists on current site, if not add them before allowing login. 
              if ( is_multisite() ) { 
                  $user_data = get_user_by( 'login', $username ); 

                  if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { 
                      add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); 
                  } 
              } 

              // Perform the login 
              $user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() ); 

              if ( is_wp_error( $user ) ) { 
                  $message = $user->get_error_message(); 
                  $message = str_replace( '<strong>' . esc_html( $creds['user_login'] ) . '</strong>', '<strong>' . esc_html( $username ) . '</strong>', $message ); 
                  throw new Exception( $message ); 
              } else { 

                  if ( ! empty( $_POST['redirect'] ) ) { 
                      $redirect = $_POST['redirect']; 
                  } elseif ( wp_get_referer() ) { 
                      $redirect = wp_get_referer(); 
                  } else { 
                      $redirect = wc_get_page_permalink( 'myaccount' ); 
                  } 

                  wp_redirect( apply_filters( 'woocommerce_login_redirect', $redirect, $user ) ); 
                  exit; 
              } 
          } catch ( Exception $e ) { 
              wc_add_notice( apply_filters( 'login_errors', $e->getMessage() ), 'error' ); 
              do_action( 'woocommerce_login_failed' ); 
          } 
      } 
  } 

推荐答案

请试试下面的代码,希望对你有帮助.将以下代码粘贴到当前活动主题 functions.php 文件中.

Please try the below code, i hope it helps you. Paste the below code in current active theme functions.php file.

// start global session for saving the referer url
function start_session() {
    if(!session_id()) {
        session_start();
    }
}
add_action('init', 'start_session', 1);

// get  referer url and save it 
function redirect_url() {
    if (! is_user_logged_in()) {
        $_SESSION['referer_url'] = wp_get_referer();
    } else {
        session_destroy();
    }
}
add_action( 'template_redirect', 'redirect_url' );

//login redirect 
function login_redirect() {
    if (isset($_SESSION['referer_url'])) {
        wp_redirect($_SESSION['referer_url']);
    } else {
        wp_redirect(home_url());
    }
}
add_filter('woocommerce_login_redirect', 'login_redirect', 1100, 2);

这篇关于WooCommerce - 登录后重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆