Jader
Jader

Reputation: 137

Woocommerce - Edit account issue

My Woocommerce is setup to generate username automatically. I'm trying to use the code below to change username before save. I would like to change user name to be equal a custom field filled in billing form.

My code is:

function wc_cpf_as_username ( $user_login ) {

        if( !empty($_POST['billing_cpf'] ) ) {
            $user_login = $_POST['billing_cpf'];
        }

        elseif (!empty( $_POST['billing_cnpj'] )){
            $user_login = $_POST['billing_cnpj'];
        }

        else{
            $user_login = $_POST['billing_email'];
        }

    return $user_login;     

}

add_filter( 'pre_user_login' , 'wc_cpf_as_username' );

The code work to create user, but this code do not work to edit user in my account page (/my-account/edit-account). Woocommerce show success message (Account details changed successfully.), but data is not changed.

I do not know what is the issue.

Could you help me?

Upvotes: 0

Views: 1002

Answers (1)

farhan ayub
farhan ayub

Reputation: 60

Why you are making that complex function if you have a hook available for this. edit_user_profile_update hook i.e. located in /wp-admin/user-edit.php.

update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']).

update_user_meta thats for update user meta field based on user ID.

 add_action('edit_user_profile_update', 'update_extra_profile_fields');


     function update_extra_profile_fields($user_id) {
         if ( current_user_can('edit_user',$user_id) )
             update_user_meta($user_id, 'Custom_field', $_POST['your_field']);
     }

Upvotes: 1

Related Questions