Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::process_customer()privateWC 1.0

Order processing relating to customer account.

Creates a customer account as needed (based on request & store settings) and updates the order with the new customer ID. Updates the order with user details (e.g. address).

Method of the class: Checkout{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->process_customer( $request );
$request(\WP_REST_Request) (required)
Request object.

Checkout::process_customer() code WC 9.3.3

private function process_customer( \WP_REST_Request $request ) {
	try {
		if ( $this->should_create_customer_account( $request ) ) {
			$customer_id = $this->create_customer_account(
				$request['billing_address']['email'],
				$request['billing_address']['first_name'],
				$request['billing_address']['last_name'],
				$request['customer_password']
			);

			// Associate customer with the order. This is done before login to ensure the order is associated with
			// the correct customer if login fails.
			$this->order->set_customer_id( $customer_id );
			$this->order->save();

			// Log the customer in to WordPress. Doing this inline instead of using `wc_set_customer_auth_cookie`
			// because wc_set_customer_auth_cookie forces the use of session cookie.
			wp_set_current_user( $customer_id );
			wp_set_auth_cookie( $customer_id, true );

			// Init session cookie if the session cookie handler exists.
			if ( is_callable( [ WC()->session, 'init_session_cookie' ] ) ) {
				WC()->session->init_session_cookie();
			}
		}
	} catch ( \Exception $error ) {
		switch ( $error->getMessage() ) {
			case 'registration-error-invalid-email':
				throw new RouteException(
					'registration-error-invalid-email',
					esc_html__( 'Please provide a valid email address.', 'woocommerce' ),
					400
				);
			case 'registration-error-email-exists':
				throw new RouteException(
					'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( $request['billing_address']['email'] )
					),
					400
				);
			case 'registration-error-empty-password':
				throw new RouteException(
					'registration-error-empty-password',
					esc_html__( 'Please create a password for your account.', 'woocommerce' ),
					400
				);
		}
	}

	// Persist customer address data to account.
	$this->order_controller->sync_customer_data_with_order( $this->order );
}