为什么在 woocommerce_login_redirect 钩子中使用时

我现在正在搜索一段时间以找到解决我的问题的方法,该函数get_current_user_id()在连接到woocommerce_login_redirect. 在登录重定向之后应该总是有一个用户 ID,或者我在这里弄错了什么?


在我的 function.php 文件中,我有以下代码:


add_action( 'woocommerce_login_redirect', 'ffaces_register_customer' ); 

add_filter( 'woocommerce_registration_redirect', 'ffaces_register_customer' );


function ffaces_register_customer( $user ) {


  $customerid = get_current_user_id();


  if ( $customerid == 0 ) {

    $redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );

    return $redirection_url; 

    exit();

  }


  // more code working with $customerid


  return home_url("my-account/my-projects");

  exit();

}


心有法竹
浏览 70回答 2
2回答

慕哥6287543

你的代码有一些错误(第一个函数参数不是用户,而是要过滤的url重定向)。请尝试以下操作:add_filter('woocommerce_login_redirect', 'ffaces_register_customer', 10, 2 );add_filter( 'woocommerce_registration_redirect', 'ffaces_register_customer', 10, 2 );function ffaces_register_customer( $redirect, $user = '' ) {    if( isset($user) && is_a( $user, 'WP_User' ) && $user->ID > 0 ) {        return home_url("my-account/my-projects");    } else {        return get_permalink( wc_get_page_id( 'myaccount' ) );    }}代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该工作。

HUH函数

我不认为它在这起火时还没有定下来。钩子传递登录用户的用户对象尝试更改$customerid = get_current_user_id();到$customerid = $user->ID;
打开App,查看更多内容
随时随地看视频慕课网APP