简体   繁体   English

Woocommerce新用户注册后更新用户元数据

[英]Update user meta data after Woocommerce new user registration

Im trying to add a custom field to WP_USERMETA table after Woocommerce registration 我试图在Woocommerce注册后向WP_USERMETA表添加自定义字段

add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);

function wc_assign_custom_role($args) {
  update_user_meta($user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));

  return $args;
}

as you see Im trying to capture password before hashing and save it in a different hash format in that table 如您所见,我试图在散列之前捕获密码并将其以另一散列格式保存在该表中

but It doesnt add anything to the table 但它不会在表中添加任何内容

I tested the same line inside wordpress registration hook user_register and it worked but only for wordpress registration not woocommerce 我在wordpress注册钩子user_register内测试了同一行,它可以工作,但仅用于wordpress注册而不是woocommerce

UPDATE UPDATE

add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);

function wc_assign_custom_role($args) {
  global $current_user;
  update_user_meta($current_user->$user_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));

  return $args;
}

still doesnt work 仍然不起作用

UPDATE II 更新二

function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) { 
    update_user_meta($customer_id, 'user_pass2', password_hash($_POST['password'], PASSWORD_DEFAULT));
}; 

add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 

this one create meta data but it seems it uses different $_POST['password'] rather than the password I entered, so hash something else rather than password 这个创建了元数据,但似乎它使用了不同的$_POST['password']而不是我输入的密码,因此对其他内容进行了哈希处理,而不是密码

Any thoughts?? 有什么想法吗??

Found the solution, We should use $_POST['account_password'] instead of $_POST['password'] 找到解决方案后,我们应该使用$_POST['account_password']而不是$_POST['password']

function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) { 
    update_user_meta($customer_id, 'user_pass2', password_hash($_POST['account_password'], PASSWORD_DEFAULT));
}; 

add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 

When looking at the source code where woocommerce_created_customer action hook is located, the password can be found as $new_customer_data['user_pass'] (see at the end of the answer) . 当查看woocommerce_created_customer操作钩子所在的源代码时,密码可以找到为$new_customer_data['user_pass'] (请参见答案末尾)

So your code should be: 因此,您的代码应为:

add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) { 
    update_user_meta($customer_id, 'user_pass2', password_hash($new_customer_data['user_pass'], PASSWORD_DEFAULT));
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 It should work. 它应该工作。


Here is the related source code involved from wc_create_new_customer() function: 这是wc_create_new_customer()函数涉及相关源代码

    $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
        'user_login' => $username,
        'user_pass'  => $password,
        'user_email' => $email,
        'role'       => 'customer',
    ) );

and $_POST['account_password'] is not required as it's already stored in $password variable. 并且不需要$_POST['account_password']因为它已经存储在$password变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM