Automattic\WooCommerce\Blocks\Utils
BlocksSharedState::get_cart_error_notices
Get cart errors formatted as notices for the store-notices interactivity store.
Returns errors from the hydrated cart state in the format expected by the store-notices store context.
Method of the class: BlocksSharedState{}
No Hooks.
Returns
Array. Array of notices with id, notice, type, and dismissible keys.
Usage
$result = BlocksSharedState::get_cart_error_notices( $consent_statement ): array;
- $consent_statement(string) (required)
- The consent statement string.
BlocksSharedState::get_cart_error_notices() BlocksSharedState::get cart error notices code WC 10.8.1
public static function get_cart_error_notices( string $consent_statement ): array {
self::check_consent( $consent_statement );
// Ensure cart state is loaded so this method works independently.
if ( null === self::$blocks_shared_cart_state ) {
self::load_cart_state( $consent_statement );
}
$errors = self::$blocks_shared_cart_state['errors'] ?? array();
$notices = array();
foreach ( $errors as $error ) {
$notices[] = array(
'id' => wp_unique_id( 'store-notice-' ),
'notice' => $error['message'] ?? '',
'type' => 'error',
'dismissible' => true,
);
}
return $notices;
}