Merge pull request #22786 from woocommerce/improve/user-registration

Improve user's display_name generation during checkout
This commit is contained in:
Mike Jolley 2019-02-27 12:55:56 +00:00 committed by GitHub
commit cc8e516f7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 14 deletions

View File

@ -965,7 +965,15 @@ class WC_Checkout {
if ( ! is_user_logged_in() && ( $this->is_registration_required() || ! empty( $data['createaccount'] ) ) ) {
$username = ! empty( $data['account_username'] ) ? $data['account_username'] : '';
$password = ! empty( $data['account_password'] ) ? $data['account_password'] : '';
$customer_id = wc_create_new_customer( $data['billing_email'], $username, $password );
$customer_id = wc_create_new_customer(
$data['billing_email'],
$username,
$password,
array(
'first_name' => ! empty( $data['billing_first_name'] ) ? $data['billing_first_name'] : '',
'last_name' => ! empty( $data['billing_last_name'] ) ? $data['billing_last_name'] : '',
)
);
if ( is_wp_error( $customer_id ) ) {
throw new Exception( $customer_id->get_error_message() );

View File

@ -32,12 +32,13 @@ if ( ! function_exists( 'wc_create_new_customer' ) ) {
/**
* Create a new customer.
*
* @param string $email Customer email.
* @param string $email Customer email.
* @param string $username Customer username.
* @param string $password Customer password.
* @param array $args List of arguments to pass to `wp_insert_user()`.
* @return int|WP_Error Returns WP_Error on failure, Int (user ID) on success.
*/
function wc_create_new_customer( $email, $username = '', $password = '' ) {
function wc_create_new_customer( $email, $username = '', $password = '', $args = array() ) {
// Check the email address.
if ( empty( $email ) || ! is_email( $email ) ) {
@ -96,11 +97,14 @@ if ( ! function_exists( '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',
array_merge(
$args,
array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
)
)
);
@ -380,9 +384,7 @@ add_filter( 'user_has_cap', 'wc_customer_has_capability', 10, 3 );
function wc_shop_manager_has_capability( $allcaps, $caps, $args, $user ) {
if ( wc_user_has_role( $user, 'shop_manager' ) ) {
/**
* @see wc_modify_map_meta_cap, which limits editing to customers.
*/
// @see wc_modify_map_meta_cap, which limits editing to customers.
$allcaps['edit_users'] = true;
}

View File

@ -1,8 +1,12 @@
<?php
/**
* Customer functions
*
* @package WooCommerce\Tests\Customer
*/
/**
* Customer functions.
* @package WooCommerce\Tests\Customer
* WC_Tests_Customer_Functions class.
*/
class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
@ -45,6 +49,19 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
$userdata = get_userdata( $id );
$this->assertEquals( 'fred2', $userdata->user_login );
// Test extra arguments to generate display_name.
$id = wc_create_new_customer(
'john.doe@example.com',
'',
'testpassword',
array(
'first_name' => 'John',
'last_name' => 'Doe',
)
);
$userdata = get_userdata( $id );
$this->assertEquals( 'John Doe', $userdata->display_name );
// No password.
update_option( 'woocommerce_registration_generate_password', 'no' );
$id = wc_create_new_customer( 'joe@example.com', 'joecustomer', '' );
@ -70,7 +87,7 @@ class WC_Tests_Customer_Functions extends WC_Unit_Test_Case {
$order2 = new WC_Order();
$order2->save();
// Test download permissions
// Test download permissions.
$prod_download = new WC_Product_Download();
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
$prod_download->set_id( 'download' );