Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::create_customer_account()privateWC 1.0

Create a new account for a customer.

The account is created with a generated username. The customer is sent an email notifying them about the account and containing a link to set their (initial) password.

Intended as a replacement for wc_create_new_customer in WC core.

Метод класса: Checkout{}

Возвращает

int. User id if successful

Использование

// private - только в коде основоного (родительского) класса
$result = $this->create_customer_account( $user_email, $first_name, $last_name );
$user_email(строка) (обязательный)
The email address to use for the new account.
$first_name(строка) (обязательный)
The first name to use for the new account.
$last_name(строка) (обязательный)
The last name to use for the new account.

Код Checkout::create_customer_account() WC 8.7.0

private function create_customer_account( $user_email, $first_name, $last_name ) {
	if ( empty( $user_email ) || ! is_email( $user_email ) ) {
		throw new \Exception( 'registration-error-invalid-email' );
	}

	if ( email_exists( $user_email ) ) {
		throw new \Exception( 'registration-error-email-exists' );
	}

	$username = wc_create_new_customer_username( $user_email );

	// Handle password creation.
	$password           = wp_generate_password();
	$password_generated = true;

	// 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, $user_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, $user_email );

	if ( is_wp_error( $errors ) && $errors->get_error_code() ) {
		throw new \Exception( $errors->get_error_code() );
	}

	/**
	 * 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',
		array(
			'user_login' => $username,
			'user_pass'  => $password,
			'user_email' => $user_email,
			'first_name' => $first_name,
			'last_name'  => $last_name,
			'role'       => 'customer',
			'source'     => 'store-api',
		)
	);

	$customer_id = wp_insert_user( $new_customer_data );

	if ( is_wp_error( $customer_id ) ) {
		throw $this->map_create_account_error( $customer_id );
	}

	// Set account flag to remind customer to update generated password.
	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;
}