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.

{} Это метод класса: NoticeHandler{}

Хуков нет.

Возвращает

\WP_Error. The WP_Error object containing all error notices.

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

$result = NoticeHandler::convert_notices_to_wp_errors( $error_code );
$error_code(строка)
Error code for the thrown exceptions.
По умолчанию: 'unknown_server_error'

Код NoticeHandler::convert_notices_to_wp_errors() WC 6.8.0

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;
}