wc_create_new_customer

Create a new customer.

Description

(int|WP_Error) wc_create_new_customer( (string) $email, (string) $username = '', (string) $password = '' ); 

Returns (int|WP_Error)

Returns WP_Error on failure, Int (user ID) on success.

Parameters (3)

0. $email (string)
Customer email.
1. $username — Optional. (string) => ''
Customer username.
2. $password — Optional. (string) => ''
Customer password.

Usage

  1. if ( !function_exists( 'wc_create_new_customer' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php'; 
  3.  
  4. // Customer email. 
  5. $email = ''; 
  6.  
  7. // Customer username. 
  8. $username = ''; 
  9.  
  10. // Customer password. 
  11. $password = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_create_new_customer($email, $username, $password); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-user-functions.php  
  1. function wc_create_new_customer( $email, $username = '', $password = '' ) { 
  2.  
  3. // Check the email address. 
  4. if ( empty( $email ) || ! is_email( $email ) ) { 
  5. return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', woocommerce ) ); 
  6.  
  7. if ( email_exists( $email ) ) { 
  8. return new WP_Error( 'registration-error-email-exists', __( 'An account is already registered with your email address. Please login.', woocommerce ) ); 
  9.  
  10. // Handle username creation. 
  11. if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) || ! empty( $username ) ) { 
  12. $username = sanitize_user( $username ); 
  13.  
  14. if ( empty( $username ) || ! validate_username( $username ) ) { 
  15. return new WP_Error( 'registration-error-invalid-username', __( 'Please enter a valid account username.', woocommerce ) ); 
  16.  
  17. if ( username_exists( $username ) ) { 
  18. return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', woocommerce ) ); 
  19. } else { 
  20. $username = sanitize_user( current( explode( '@', $email ) ), true ); 
  21.  
  22. // Ensure username is unique. 
  23. $append = 1; 
  24. $o_username = $username; 
  25.  
  26. while ( username_exists( $username ) ) { 
  27. $username = $o_username . $append; 
  28. $append++; 
  29.  
  30. // Handle password creation. 
  31. if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) ) { 
  32. $password = wp_generate_password(); 
  33. $password_generated = true; 
  34. } elseif ( empty( $password ) ) { 
  35. return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', woocommerce ) ); 
  36. } else { 
  37. $password_generated = false; 
  38.  
  39. // Use WP_Error to handle registration errors. 
  40. $errors = new WP_Error(); 
  41.  
  42. do_action( 'woocommerce_register_post', $username, $email, $errors ); 
  43.  
  44. $errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $email ); 
  45.  
  46. if ( $errors->get_error_code() ) { 
  47. return $errors; 
  48.  
  49. $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array( 
  50. 'user_login' => $username,  
  51. 'user_pass' => $password,  
  52. 'user_email' => $email,  
  53. 'role' => 'customer',  
  54. ) ); 
  55.  
  56. $customer_id = wp_insert_user( $new_customer_data ); 
  57.  
  58. if ( is_wp_error( $customer_id ) ) { 
  59. return new WP_Error( 'registration-error', '<strong>' . __( 'Error:', woocommerce ) . '</strong> ' . __( 'Couldn’t register you… please contact us if you continue to have problems.', woocommerce ) ); 
  60.  
  61. do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); 
  62.  
  63. return $customer_id;