session->get( 'wc_notices', array() ); if ( isset( $all_notices[$notice_type] ) ) { $notice_count = absint( sizeof( $all_notices[$notice_type] ) ); } elseif ( empty( $notice_type ) ) { foreach ( $all_notices as $notices ) { $notice_count += absint( sizeof( $all_notices ) ); } } return $notice_count; } /** * Add and store a notice * * @param string $message The text to display in the notice. * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] */ function wc_add_notice( $message, $notice_type = 'success' ) { $notices = WC()->session->get( 'wc_notices', array() ); // Backward compatibility if ( 'success' === $notice_type ) $message = apply_filters( 'woocommerce_add_message', $message ); $notices[$notice_type][] = apply_filters( 'woocommerce_add_' . $notice_type, $message ); WC()->session->set( 'wc_notices', $notices ); } /** * Unset all notices * * @since 2.1 */ function wc_clear_notices() { WC()->session->set( 'wc_notices', null ); } /** * Prints messages and errors which are stored in the session, then clears them. * * @since 2.1 */ function wc_print_notices() { $all_notices = WC()->session->get( 'wc_notices', array() ); $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) ); foreach ( $notice_types as $notice_type ) { if ( wc_notice_count( $notice_type ) > 0 ) { woocommerce_get_template( "notices/{$notice_type}.php", array( 'messages' => $all_notices[$notice_type] ) ); } } wc_clear_notices(); } add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 ); add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); /** * Print a single notice immediately * * @param string $message The text to display in the notice. * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] */ function wc_print_notice( $message, $notice_type = 'success' ) { if ( 'success' === $notice_type ) $message = apply_filters( 'woocommerce_add_message', $message ); woocommerce_get_template( "notices/{$notice_type}.php", array( 'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ) ) ); } /** * Returns all queued notices, optionally filtered by a notice type. * * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] */ function wc_get_notices( $notice_type = '' ) { $all_notices = WC()->session->get( 'wc_notices', array() ); if ( empty ( $notice_type ) ) { $notices = $all_notices; } elseif ( isset( $all_notices[$notice_type] ) ) { $notices = $all_notices[$notice_type]; } else { $notices = array(); } return $notices; }