WooCommerce - register second user in woocommerce_created_customer hook

Viewed 161

I'm coming across an issue when trying to register a second user account once someone registers as a customer via WooCommerce. I have added the following woocommerce_created_customer hook:

add_action('woocommerce_created_customer', function($customer_id)
{
    if(isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']))
    {
        $createSecondUserId = wp_create_user(strtolower($_POST['second_user_first_name'].'-'.$_POST['second_user_last_name']).'-'.$customer_id, wp_generate_password(), 'test@test.com');

        if(is_wp_error($createSecondUserId))
        {
            $errors = $createSecondUserId->errors;

            print_r($errors);
            die();
        }
    }
});

However I get the following error when submitting a new WooCommerce registration:

Array ( [existing_user_login] => Array ( [0] => Sorry, that username already exists! ) )

It's strange as I'm setting a random username within the wp_create_user function, so the usernames should not clash. Has anyone got any ideas?

2 Answers

You can use username_exists() to determines that the given username exists.

add_action( 'woocommerce_created_customer', function($customer_id){

    if( isset( $_POST['second_user_first_name'] ) && isset( $_POST['second_user_last_name'] ) ) {

        if( !username_exists( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id ) ){

            $createSecondUserId = wp_create_user( strtolower( $_POST['second_user_first_name'].'-'.$_POST['second_user_last_name'] ).'-'.$customer_id, wp_generate_password(), 'test@test.com' );

            if(is_wp_error($createSecondUserId)){

                $errors = $createSecondUserId->errors;
                print_r($errors);
                die();

            }

        }

    }

});

If the username already exists you can create a new one by adding a progressive numeric suffix. In this way you will be sure that the username of the second account will always be unique.

Note that if you run multiple tests with your current code you need to make sure you remove the user with the email address test@test.com otherwise you will get an error: [existing_user_email] => Sorry, that email address is already used!.

add_action('woocommerce_created_customer', function( $customer_id ) {
    if ( isset($_POST['second_user_first_name']) && isset($_POST['second_user_last_name']) ) {
        // create the username based on the form data
        $username = strtolower( $_POST['second_user_first_name'] . '-' . $_POST['second_user_last_name'] ) . '-' . $customer_id;
        // if the username already exists it creates a unique one
        if ( username_exists($username) ) {
            $i = 0;
            while ( username_exists($username) ) {
                $username = $username . '-' . ++$i;
            }
        }
        // create the second user
        $createSecondUserId = wp_create_user( $username, wp_generate_password(), 'test@test.com' );
    }
});

The code has been tested and works. Add it to your active theme's functions.php.

Related