The WooCommerce 2.1 notice API

If you’ve built a plugin for WooCommerce before you may have come across the ‘message’ functionality which let you add messages (or errors) to the frontend using:

$woocommerce->add_message( 'Hi there' )
$woocommerce->add_error( 'You bafoon' );

Part of the development of WooCommerce 2.1 was refactoring the main WooCommerce class which involved moving out unrelated methods (such as the messages) and moving them into their own classes.

We’ve replaced this particular functionality with some new functions and a new notices API which is much more flexible. The old methods are deprecated.

Understanding notice types

The first major change if the introduction of notice types. In 2.0.x there were two types; message and error, and these had there own methods making it very inflexible. 2.1 introduces notice types meaning you can have as many different types as you want. 2.1 itself uses the following three:


A ‘success’ message when something is successful, for example after adding an item to the cart, or getting a shipping quote.



An ‘error’ message triggered when something goes wrong, either user fault or code.



A generic notice type which is neither an error nor a success message.


Adding a notice

The main new function is wc_add_notice. This is called passing the message itself, and the notice type:

wc_add_notice( $message, $notice_type = 'success' );

Once added, the notice is queued and will be output and cleared when WooCommerce outputs notices before a loop.

Templates used by notices

As mentioned earlier, there are three types used by WooCommerce core – each of these types have a template file:

  1. templates/notice/error.php
  2. templates/notice/notice.php
  3. templates/notice/success.php

Each template outputs a list of notices of that particular type, and you can of course make your own types and use your own template files should the default three types not cover your needs.

Other Notice functions

Aside from adding notices, the API has several other notices related functions – these include:

  1. wc_notice_count( $notice_type ) – Returns the count of queued notices of the type you specify.
  2. wc_get_notices( $notice_type ) – Returns all queued notices (in an array) of the type you specify.
  3. wc_clear_notices – Cleared all queued notices.
  4. wc_print_notices – Outputs all queued notices. This is hooked in to woocommerce_before_shop_loop and woocommerce_before_single_product.
  5. wc_print_notice( $message, $notice_type = 'success' ) – Works in a similar wasy to wc_add_notice, however rather than queuing the notice, it outputs it immediately.

Using the new API

If you are developing for 2.1 only, these functions should be used going forward. If however you are still maintaining 2.0 support you will need to do a function_exists() check prior to use.

Good luck with the new API 🙂





7 responses to “The WooCommerce 2.1 notice API”

  1. Ciaran avatar

    Hi there,

    I was using the following to remove the messages:

    remove_action( ‘woocommerce_before_single_product’, ‘woocommerce_show_messages’ );

    and then calling them in my theme using the following:

    How can i replicate this functionality with the new notice API?


  2. Srihari Thalla avatar

    Thanks, the templates are what I am looking for!! Now able to tweak them using a Child Theme!!

  3. evolutionsigns avatar

    Hello Mike – Thank you for posting this. Is it possible to create a queued message for a specific user from an admin side script to be viewed when the user next logs in or is the queue strictly for the current session?

  4. Vincent avatar


    I would like to change the notice button for success. I need to change button class. How to do this ?

    Thanks for your reply.


  5. bryceadams avatar

    Hey Mike.. you missed an apostrophe in line 2 of the 1st example 🙂

    $woocommerce->add_error( ‘You bafoon );
    $woocommerce->add_error( ‘You bafoon’ );

    1. mikejolley avatar


  6. Liam Bailey avatar
    Liam Bailey

    Hi Mike – I know this is probably an old post, but I have found it while searching. I am finding some strange problems with the new notice api, like now I just tried to hook wc_print_notices to woocommerce_before_my_account and no dice – no output at all. And I know that the notice was being added because an if condition takes me to one page or my account page, the other page displays the notice being added, but not my account page. I didn’t do var_dump or anything on it, and error reporting wasn’t on, to be honest it wasn’t that important, but I just wondered why this would be. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: