Automattic\WooCommerce\StoreApi\Utilities

NoticeHandler::convert_notices_to_wp_errors()public staticWC 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.

This method will discard notices once complete.

Method of the class: NoticeHandler{}

No Hooks.

Return

\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 the thrown exceptions.
Default: 'unknown_server_error'

NoticeHandler::convert_notices_to_wp_errors() code WC 9.3.3

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

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

	$error_notices = wc_get_notices( 'error' );

	// Prevent notices from being output later on.
	wc_clear_notices();

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

	return $errors;
}