تابع ووکامرسی wc_create_new_customer افزودن مشتری یا کاربر جدید به فروشگاه

تابع ووکامرسی wc_create_new_customer – افزودن مشتری یا کاربر جدید به فروشگاه
Syntax – سینتکس
(int|WP_Error) wc_create_new_customer( (string) $email, (string) $username = '', (string) $password = '' );
Parameters – پارامتر ها (3)
- 1- $email (string)
- 2- $username (string)
- 3- $password (string)
Returns – مقادیر بازگشتی (int|WP_Error)
Returns WP_Error on failure, Int (user ID) on success.
Usage – نحوه استفاده
if ( !function_exists( 'wc_create_new_customer' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php';
}
// Customer email.
$email = '';
// Customer username.
$username = '';
// Customer password.
$password = '';
// NOTICE! Understand what this does before running.
$result = wc_create_new_customer($email, $username, $password);
Defined – محل تعریف
/includes/wc-user-functions.php
function wc_create_new_customer( $email, $username = '', $password = '' ) {
// Check the email address.
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', __( 'An account is already registered with your email address. Please login.', woocommerce ) );
}
// Handle username creation.
if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) || ! empty( $username ) ) {
$username = sanitize_user( $username );
if ( empty( $username ) || ! validate_username( $username ) ) {
return new WP_Error( 'registration-error-invalid-username', __( 'Please enter 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 ) );
}
} else {
$username = sanitize_user( current( explode( '@', $email ) ), true );
// Ensure username is unique.
$append = 1;
$o_username = $username;
while ( username_exists( $username ) ) {
$username = $o_username . $append;
$append++;
}
}
// Handle password creation.
if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) ) {
$password = wp_generate_password();
$password_generated = true;
} elseif ( empty( $password ) ) {
return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', woocommerce ) );
} else {
$password_generated = false;
}
// Use WP_Error to handle registration errors.
$errors = new WP_Error();
do_action( 'woocommerce_register_post', $username, $email, $errors );
$errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $email );
if ( $errors->get_error_code() ) {
return $errors;
}
$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
) );
$customer_id = wp_insert_user( $new_customer_data );
if ( is_wp_error( $customer_id ) ) {
return new WP_Error( 'registration-error', '' . __( 'Error:', woocommerce ) . ' ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', woocommerce ) );
}
do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
return $customer_id;
}
versions – نسخه ها
از نسخه : 3.0.2
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر