为了账号安全,请及时绑定邮箱和手机立即绑定

为什么在 woocommerce_login_redirect 钩子中使用时

为什么在 woocommerce_login_redirect 钩子中使用时

PHP
心有法竹 2023-04-28 14:21:42
我现在正在搜索一段时间以找到解决我的问题的方法,该函数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();}
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

你的代码有一些错误(第一个函数参数不是用户,而是要过滤的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 文件。它应该工作。


查看完整回答
反对 回复 2023-04-28
?
HUH函数

TA贡献1836条经验 获得超4个赞

我不认为它在这起火时还没有定下来。

钩子传递登录用户的用户对象尝试更改

$customerid = get_current_user_id();

$customerid = $user->ID;


查看完整回答
反对 回复 2023-04-28
  • 2 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信