Advertisement
verygoodplugins

Untitled

Mar 18th, 2021
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1.  
  2. /**
  3.  * Create a new customer.
  4.  *
  5.  * @param  string $email    Customer email.
  6.  * @param  string $username Customer username.
  7.  * @param  string $password Customer password.
  8.  * @param  array  $args     List of arguments to pass to `wp_insert_user()`.
  9.  * @return int|WP_Error Returns WP_Error on failure, Int (user ID) on success.
  10.  */
  11. function wc_create_new_customer( $email, $username = '', $password = '', $args = array() ) {
  12.     if ( empty( $email ) || ! is_email( $email ) ) {
  13.         return new WP_Error( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'woocommerce' ) );
  14.     }
  15.  
  16.     if ( email_exists( $email ) ) {
  17.         // REMOVED: return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'An account is already registered with your email address. <a href="#" class="showlogin">Please log in.</a>', 'woocommerce' ), $email ) );
  18.  
  19.         $user = get_user_by( 'email', $email ); // ADDED
  20.         return $user->ID; // ADDED
  21.  
  22.     }
  23.  
  24.     if ( 'yes' === get_option( 'woocommerce_registration_generate_username', 'yes' ) && empty( $username ) ) {
  25.         $username = wc_create_new_customer_username( $email, $args );
  26.     }
  27.  
  28.     $username = sanitize_user( $username );
  29.  
  30.     if ( empty( $username ) || ! validate_username( $username ) ) {
  31.         return new WP_Error( 'registration-error-invalid-username', __( 'Please enter a valid account username.', 'woocommerce' ) );
  32.     }
  33.  
  34.     if ( username_exists( $username ) ) {
  35.         return new WP_Error( 'registration-error-username-exists', __( 'An account is already registered with that username. Please choose another.', 'woocommerce' ) );
  36.     }
  37.  
  38.     // Handle password creation.
  39.     $password_generated = false;
  40.     if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && empty( $password ) ) {
  41.         $password           = wp_generate_password();
  42.         $password_generated = true;
  43.     }
  44.  
  45.     if ( empty( $password ) ) {
  46.         return new WP_Error( 'registration-error-missing-password', __( 'Please enter an account password.', 'woocommerce' ) );
  47.     }
  48.  
  49.     // Use WP_Error to handle registration errors.
  50.     $errors = new WP_Error();
  51.  
  52.     do_action( 'woocommerce_register_post', $username, $email, $errors );
  53.  
  54.     $errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $email );
  55.  
  56.     if ( $errors->get_error_code() ) {
  57.         return $errors;
  58.     }
  59.  
  60.     $new_customer_data = apply_filters(
  61.         'woocommerce_new_customer_data',
  62.         array_merge(
  63.             $args,
  64.             array(
  65.                 'user_login' => $username,
  66.                 'user_pass'  => $password,
  67.                 'user_email' => $email,
  68.                 'role'       => 'customer',
  69.             )
  70.         )
  71.     );
  72.  
  73.     $customer_id = wp_insert_user( $new_customer_data );
  74.  
  75.     if ( is_wp_error( $customer_id ) ) {
  76.         return $customer_id;
  77.     }
  78.  
  79.     do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
  80.  
  81.     return $customer_id;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement