简体   繁体   中英

Update user meta data after Woocommerce new user registration

Im trying to add a custom field to WP_USERMETA table after Woocommerce registration

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

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

Any thoughts??

Found the solution, We should use $_POST['account_password'] instead of $_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) .

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). It should work.


Here is the related source code involved from wc_create_new_customer() function:

    $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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