Automattic\WooCommerce\Blocks\BlockTypes\OrderConfirmation

CreateAccount::process_form_postprotectedWC 1.0

Process posted account form.

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

Хуков нет.

Возвращает

\WP_Error|int.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->process_form_post( $order );
$order(WC_Order) (обязательный)
Order object.

Код CreateAccount::process_form_post() WC 9.9.4

protected function process_form_post( $order ) {
	if ( ! isset( $_POST['create-account'], $_POST['email'], $_POST['_wpnonce'] ) ) {
		return 0;
	}

	if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ?? '' ) ), 'wc_create_account' ) ) {
		return new \WP_Error( 'invalid_nonce', __( 'Unable to create account. Please try again.', 'woocommerce' ) );
	}

	$user_email = sanitize_email( wp_unslash( $_POST['email'] ) );

	// Does order already have user?
	if ( $order->get_customer_id() ) {
		return new \WP_Error( 'order_already_has_user', __( 'This order is already linked to a user account.', 'woocommerce' ) );
	}

	// Check given details match the current viewed order.
	if ( $order->get_billing_email() !== $user_email ) {
		return new \WP_Error( 'email_mismatch', __( 'The email address provided does not match the email address on this order.', 'woocommerce' ) );
	}

	$generate_password = filter_var( get_option( 'woocommerce_registration_generate_password', 'no' ), FILTER_VALIDATE_BOOLEAN );

	if ( $generate_password ) {
		$password = ''; // Will be generated by wc_create_new_customer.
	} else {
		$password = wp_unslash( $_POST['password'] ?? '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		if ( empty( $password ) || strlen( $password ) < 8 ) {
			return new \WP_Error( 'password_too_short', __( 'Password must be at least 8 characters.', 'woocommerce' ) );
		}
	}

	$customer_id = wc_create_new_customer(
		$user_email,
		'',
		$password,
		[
			'first_name' => $order->get_billing_first_name(),
			'last_name'  => $order->get_billing_last_name(),
			'source'     => 'delayed-account-creation',
		]
	);

	if ( is_wp_error( $customer_id ) ) {
		return $customer_id;
	}

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

	// Associate addresses from the order with the customer.
	$order_controller = new OrderController();
	$order_controller->sync_customer_data_with_order( $order );

	// Set the customer auth cookie.
	wc_set_customer_auth_cookie( $customer_id );

	return $customer_id;
}