Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFieldsFrontend::update_additional_fields_for_customer()protectedWC 1.0

Validate and save additional fields for a given customer.

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

Хуков нет.

Возвращает

true|\WP_Error. True if successful, \WP_Error if there are errors.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->update_additional_fields_for_customer( $customer, $location, $group );
$customer(WC_Customer) (обязательный)
Customer object.
$location(строка) (обязательный)
Location to save fields for.
$group(строка) (обязательный)
Group to save fields for.

Код CheckoutFieldsFrontend::update_additional_fields_for_customer() WC 9.8.5

protected function update_additional_fields_for_customer( $customer, $location, $group ) {
	// Get all values from the POST request before validating.
	$field_values           = $this->get_posted_additional_field_values( $location, $group, false ); // These values are used to see if required fields have values.
	$sanitized_field_values = $this->get_posted_additional_field_values( $location, $group ); // These values are used to validate custom rules, generate the document object, and save fields to the account.

	if ( Features::is_enabled( 'experimental-blocks' ) ) {
		$document_object = new DocumentObject(
			[
				'customer' => [
					( 'address' === $location ? $group . '_address' : 'additional_fields' ) => $sanitized_field_values,
				],
			]
		);
		$document_object->set_customer( $customer );
		$document_object->set_context( 'address' === $location ? $group . '_address' : $location );
		$fields = $this->checkout_fields_controller->get_contextual_fields_for_location( $location, $document_object );
	} else {
		$fields = $this->checkout_fields_controller->get_fields_for_location( $location );
	}

	// Holds values to be persisted to the customer object.
	$persist_fields = [];
	$errors         = new \WP_Error();

	// Validate individual fields agains the document object. Errors are added to the $errors object, and each field is validated regardless of other field errors.
	foreach ( $fields as $field_key => $field ) {
		$field_value = $field_values[ $field_key ];

		if ( empty( $field_value ) ) {
			if ( ! empty( $field['required'] ) ) {
				$errors->add(
					'required_field',
					/* translators: %s: is the field label */
					sprintf( __( '%s is required', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' )
				);
				continue;
			}
			$persist_fields[ $field_key ] = '';
			continue;
		}

		$sanitized_field_value = $sanitized_field_values[ $field_key ];
		$valid_check           = $this->checkout_fields_controller->validate_field( $field, $sanitized_field_value );

		if ( is_wp_error( $valid_check ) && $valid_check->has_errors() ) {
			// Get one error message from the WP_Error object per field to avoid overlapping error messages.
			$errors->add( $valid_check->get_error_code(), $valid_check->get_error_message() );
			continue;
		}

		$persist_fields[ $field_key ] = $sanitized_field_value;
	}

	// Validate all fields for this location (this runs custom validation callbacks). If an error is found, no values will be persisted to the customer object.
	$location_validation = $this->checkout_fields_controller->validate_fields_for_location( $sanitized_field_values, $location, $group );

	if ( is_wp_error( $location_validation ) && $location_validation->has_errors() ) {
		$errors->merge_from( $location_validation );
		return $errors;
	}

	foreach ( $persist_fields as $field_key => $field_value ) {
		$this->checkout_fields_controller->persist_field_for_customer( $field_key, $field_value, $customer, $group );
	}

	return $errors->has_errors() ? $errors : true;
}