Retrieve validation errors from notices

This commit is contained in:
Alex P 2023-02-03 10:36:48 +02:00
parent d6bd403b5a
commit 838978fcf6
No known key found for this signature in database
GPG key ID: 54487A734A204D71

View file

@ -42,8 +42,26 @@ class CheckoutFormValidator extends WC_Checkout {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@$this->validate_checkout( $data, $errors );
if ( $errors->has_errors() ) {
throw new ValidationException( $errors->get_error_messages() );
// Some plugins call wc_add_notice directly.
// We should retrieve such notices, and also clear them to avoid duplicates later.
// TODO: Normally WC converts the messages from validate_checkout into notices,
// maybe we should do the same for consistency, but it requires lots of changes in the way we handle/output errors.
$messages = array_merge(
$errors->get_error_messages(),
array_map(
function ( array $notice ): string {
return $notice['notice'];
},
wc_get_notices( 'error' )
)
);
if ( wc_notice_count( 'error' ) > 0 ) {
wc_clear_notices();
}
if ( $messages ) {
throw new ValidationException( $messages );
}
}
}