wc_create_new_customer() │ WC 9.4.0
Create a new customer.
Возвращает
int|WP_Error
. Returns WP_Error on failure, Int (user ID) on success.
Использование
wc_create_new_customer( $email, $username, $password, $args );
- $email(строка) (обязательный)
- Customer email.
- $username(строка)
- Customer username.
По умолчанию: ''
- $password(строка)
- Customer password.
По умолчанию: ''
- $args(массив)
- List of arguments to pass to wp_insert_user().
По умолчанию: array()
Список изменений
С версии 9.4.0 |
Введена. |
С версии 9.4.0 |
Moved woocommerce_registration_error_email_exists filter to the shortcode checkout class. |
С версии 9.4.0 |
Removed handling for generating username/password based on settings--this is consumed at form level. Here, if data is missing it will be generated. |
Код wc_create_new_customer() wc create new customer
WC 9.8.1
function wc_create_new_customer( $email, $username = '', $password = '', $args = array() ) {
if ( empty( $email ) || ! is_email( $email ) ) {
return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'woocommerce' ) );
}
if ( email_exists( $email ) ) {
return new WP_Error(
'registration-error-email-exists',
sprintf(
// Translators: %s Email address.
esc_html__( 'An account is already registered with %s. Please log in or use a different email address.', 'woocommerce' ),
esc_html( $email )
)
);
}
if ( empty( $username ) ) {
$username = wc_create_new_customer_username( $email, $args );
}
$username = sanitize_user( $username );
if ( empty( $username ) || ! validate_username( $username ) ) {
return new WP_Error( 'registration-error-invalid-username', __( 'Please provide a valid account username.', 'woocommerce' ) );
}
if ( username_exists( $username ) ) {
return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) );
}
// Handle password creation.
$password_generated = false;
if ( empty( $password ) ) {
$password = wp_generate_password();
$password_generated = true;
}
if ( empty( $password ) ) {
return new WP_Error( 'registration-error-missing-password', __( 'Please create a password for your account.', 'woocommerce' ) );
}
// Use WP_Error to handle registration errors.
$errors = new WP_Error();
/**
* Fires before a customer account is registered.
*
* This hook fires before customer accounts are created and passes the form data (username, email) and an array
* of errors.
*
* This could be used to add extra validation logic and append errors to the array.
*
* @since 7.2.0
*
* @internal Matches filter name in WooCommerce core.
*
* @param string $username Customer username.
* @param string $user_email Customer email address.
* @param \WP_Error $errors Error object.
*/
do_action( 'woocommerce_register_post', $username, $email, $errors );
/**
* Filters registration errors before a customer account is registered.
*
* This hook filters registration errors. This can be used to manipulate the array of errors before
* they are displayed.
*
* @since 7.2.0
*
* @internal Matches filter name in WooCommerce core.
*
* @param \WP_Error $errors Error object.
* @param string $username Customer username.
* @param string $user_email Customer email address.
* @return \WP_Error
*/
$errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $email );
if ( is_wp_error( $errors ) && $errors->get_error_code() ) {
return $errors;
}
// Merged passed args with sanitized username, email, and password.
$customer_data = array_merge(
$args,
array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
)
);
/**
* Filters customer data before a customer account is registered.
*
* This hook filters customer data. It allows user data to be changed, for example, username, password, email,
* first name, last name, and role.
*
* @since 7.2.0
*
* @param array $customer_data An array of customer (user) data.
* @return array
*/
$new_customer_data = apply_filters(
'woocommerce_new_customer_data',
wp_parse_args(
$customer_data,
array(
'first_name' => '',
'last_name' => '',
'source' => 'unknown',
)
)
);
$customer_id = wp_insert_user( $new_customer_data );
if ( is_wp_error( $customer_id ) ) {
return $customer_id;
}
// Set account flag to remind customer to update generated password.
if ( $password_generated ) {
update_user_option( $customer_id, 'default_password_nag', true, true );
}
/**
* Fires after a customer account has been registered.
*
* This hook fires after customer accounts are created and passes the customer data.
*
* @since 7.2.0
*
* @internal Matches filter name in WooCommerce core.
*
* @param integer $customer_id New customer (user) ID.
* @param array $new_customer_data Array of customer (user) data.
* @param string $password_generated The generated password for the account.
*/
do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
return $customer_id;
}