? $group . '_address' : $location ); $fields = $this->checkout_fields_controller->get_contextual_fields_for_location( $location, $document_object ); // 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 ( true === $field['required'] ) { $errors->add( 'required_field', /* translators: %s: is the field label */ sprintf( __( '%s is required', 'woocommerce' ), '' . $field['label'] . '' ) ); 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; } }