Wrong username and displayname after user registraton WooCommerce

When people are buying a product through the WooCommerce extension, I have enabled that they can create an account on checkout. I don’t let them choose their own username and password.

Now I see that WP uses some part of the emailaddress to create a username, and the display name is based on the first name filled in on checkout.

Read More

Now I want to change that:

  • username must be the complete emailaddress [SOLVED];
  • display name must be the first name + last name [UNSOLVED].

I tried this: for display name with my ‘no knowledge’ of PHP:

add_filter('pre_user_display_name', 'wsis_pre_user_display_name');
function wsis_pre_user_display_name($display_name) {
    $first = get_user_field("billing_first_name"); 
    $last = get_user_field("billing_last_name"); 
    $display_name = $first . $last;

    return $display_name;
}

This filter is mentioned in their codex: http://codex.wordpress.org/Function_Reference/wp_update_user. But no examples and my code didn’t work. Anybody can help?

In user.php wp-includes folder I found the filter, now get it working :-).

/**
     * Filter a user's display name before the user is created or updated.
     *
     * @since 2.0.3
     *
     * @param string $display_name The user's display name.
     */
    $display_name = apply_filters( 'pre_user_display_name', $display_name );

    if ( empty($description) )
        $description = '';

Second try did something! It left the display name completely empty:

add_filter('pre_user_display_name', 'pre_user_display_name');
function pre_user_display_name($display_name) {
    $first = get_the_author_meta('first_name'); 
    $last = get_the_author_meta('last_name'); 

    $display_name = $first . $last;

    return $display_name;
}

Am I on the right track?

Related posts

Leave a Reply

2 comments

  1. To set the username as complete email address, you can add following code in functions.php file of your theme.

    add_filter('woocommerce_new_customer_data','change_username');
    
    function change_username($user_data)
            {
               return array(
                          'user_login' => $user_data['user_email'],
                          'user_pass'  => $user_data['user_pass'],
                          'user_email' => $user_data['user_email'],
                          'role'       => 'customer'
    
                       );
            }
    
  2. Here is how to set the display name to first name + last name.

    // Sets the display name to first name and last name
    add_filter( 'pre_user_display_name' , 'default_display_name' );
    function default_display_name($name) {
        if ( isset( $_POST['billing_first_name'] ) ) {
            $firstname = sanitize_text_field( $_POST['billing_first_name'] );
        }
        if ( isset( $_POST['billing_last_name'] ) ) {
            $lastname = sanitize_text_field( $_POST['billing_last_name'] );
        }
        $name = $firstname . ' ' . $lastname;
        return $name;
    }
    

    Source : http://geektamin.com/blog/533/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead/