Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::process_customerprivateWC 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.

Returns

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 10.8.1

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

		if ( is_wp_error( $customer_id ) ) {
			throw new RouteException(
				esc_html( $customer_id->get_error_code() ),
				esc_html( $customer_id->get_error_message() ),
				400
			);
		}

		// Associate customer with the order.
		$this->order->set_customer_id( $customer_id );
		$this->order->save();

		// Set the customer auth cookie.
		wc_set_customer_auth_cookie( $customer_id );
		wc_log_order_step( '[Store API #6::process_customer] Created new customer', array( 'customer_id' => $customer_id ) );

	}

	// Persist customer address data to account.
	$this->order_controller->sync_customer_data_with_order( $this->order );
	wc_log_order_step( '[Store API #6::process_customer] Synced customer data from order', array( 'customer_id' => $this->order->get_customer_id() ) );
}