Automattic\WooCommerce\StoreApi\Utilities

NoticeHandler::convert_notices_to_wp_errors │ public static │ WC 1.0

Collects queued error notices into a \WP_Error.

For example, cart validation processes may add error notices to prevent checkout. Since we're not rendering notices at all, we need to catch them and group them in a single WP_Error instance.

Unlike convert_notices_to_exceptions(), this does not clear the notice queue. Callers that need it cleared are responsible for doing so.

Method of the class: NoticeHandler{}

No Hooks.

Returns

\WP_Error. The WP_Error object containing all error notices.

Usage

$result = NoticeHandler::convert_notices_to_wp_errors( $error_code );
$error_code(string)
Error code for each notice added to the WP_Error object.
Default: 'unknown_server_error'

NoticeHandler::convert_notices_to_wp_errors() code WC 11.2.1

public static function convert_notices_to_wp_errors( $error_code = 'unknown_server_error' ) {
	$errors = new WP_Error();

	if ( 0 === wc_notice_count( 'error' ) ) {
		return $errors;
	}

	$error_notices = wc_get_notices( 'error' );

	foreach ( $error_notices as $error_notice ) {
		$errors->add( $error_code, wp_strip_all_tags( $error_notice['notice'] ) );
	}

	return $errors;
}