Utilizing Magento notification system


Building your own Magento modules (extensions) can be a tedious task. We always strive to develop everything ‘the Magento way’, which is why we frequently dig into the core to see how Magento’s Core team does things. Once in a while, we stumble upon useful features we think are useful and share them with you. One of such features is Magento’s notification system.

If you’re wondering what I’m talking about, I’ve got an example for you in the image below:


These messages, printed as notices, warnings or upon successful / unsuccessful action can be very useful. With this said, we have a few notification types in Magento we can use:


Mage::getSingleton(‘core/session’)->addNotice(‘Notice message’);notice image


Mage::getSingleton(‘core/session’)->addSuccess(‘Success message’); success image


Mage::getSingleton(‘core/session’)->addError(‘Error message’); error image

Warning (admin only)

Mage::getSingleton(‘adminhtml/session’)->addWarning(‘Warning message’); warning-image

Output errors from a controller

As there is no point in calling any of these from your view files, i’ll show you an example of setting such error message from a controller.

class Inchoo_Notifications_IndexController extends Mage_Core_Controller_Front_Action
	public function sendAction()
			$mail = new Zend_Mail();
		catch (Exception $e)
			Mage::getSingleton('core/session')->addError('Error sending mail');

Note that the $mail object doesn’t have any parameters set (subject, recipient, etc.). This is intentional, so we get our exception section to fire off. When you visit the URL of this controller’s action, you should get something like this:

notification image example

You could use this kind of error reporting, not only for printing messages for customers from your custom controllers, but also during development. You could also append an exception message:

catch (Exception $e)
	Mage::getSingleton('core/session')->addError('Error sending mail ' . $e->getMessage());

This is pretty much it from working examples for notification system. Play with it a little, it can be pretty useful. If you want to get an insight as to what makes this magic happen, continue reading.

Under the hood

What part of the code actually outputs these messages? Well, if you were to open lets say app/design/frontend/base/default/template/page/3columns.phtml file, you would find something like this:

//line 47 starts below
<div class="col-main">
	<?php echo $this->getChildHtml('global_messages') ?>
	<?php echo $this->getChildHtml('content') ?>

Notice the $this->getChildHtml(‘global_messages’) section? The getChildHtml() method is calling a block with name set to ‘global_messages‘, from it’s page layout file, in this case – page.xml. Let’s open it.


//line 89 starts below
<block type="core/messages" name="global_messages" as="global_messages"/>

As we can see, the block being called here has a class of Mage_Core_Block_Messages. Let’s go there, to get deeper insight as to what happens. Logic for adding messages is here:

//line 152 starts below
public function addError($message)
	return $this;

While we retrieve messages when this block prepares it’s layout:

//line 78 starts below
public function _prepareLayout()

If we trace getMessages method, we’ll end up in Mage_Core_Model_Session_Abstract. It’s getMessages() method is what retrieves messages we have stored in the session. A good example of using different class than ‘core/session‘ for notification messages is Mage::getSingleton(‘catalog/session’)->addNotice(‘Notice text’). Here we see the use of ‘catalog/session‘ model (class Mage_Catalog_Model_Session). Let’s examine this model’s constructor:

//line 36 starts below
public function __construct()

Tracing the init() method we’ll end up looking at Mage_Core_Model_Session_Abstract.

//line 82 starts below
public function init($namespace, $sessionName=null)
	parent::init($namespace, $sessionName);
	return $this;

It’s init() method tells us that this storage mechanism is merely using different session namespace to store notification messages. On one side this can be seen as another way of complicating things while, but, on the other hand, it can be nice way of organizing things.

This kind of flexibility can be great, as you’re not limited to one storage mechanism. Instead of using Mage::getSingleton(‘core/session’), you could use something like Mage::getSingleton(‘myclass/my_model’).

You are also very flexible when it comes to outputting those notification messages. For instance, if we open catalog product view page: app/design/frontend/base/default/template/catalog/product/view.phtml

//line 39 starts below
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>

As we’re actually printing messages here, you could move this PHP code in your phtml file wherever you see fit.

That’s it for Magento’s notification system. As with everything, if you want to get a deeper insight as to what’s happening with some part of Magento, tracing the code is a way to go. If you’re not sure how to do so or need a few tips&tricks which can improve the overall state of your store, make sure to check out our technical audit – it’s designed to help you!


  1. This is old but on the off chance you are still monitoring this– is it possible to get at these default error / notice / success / etc messages in the regular magento Admin section, as opposed to having to go into the code? I am really a newbie so seeking easy access. Thanks

  2. This post is a bit old, but in case somebody needs to use this messages in a multilanguage magento, you can use the Magento default translation system:

    Mage::getSingleton(‘checkout/session’)->addNotice(Mage::helper(‘core’)->__(‘Your String Here’);

    And add the translation in the translate.csv file as usual. The same goes if you need to show variable with your message:

    Mage::getSingleton(‘checkout/session’)->addNotice(Mage::helper(‘core’)->__(‘Your String Here %s and Here %s’, $firstvariable, $secondvariable);

    And in the translate.csv file:

    “Your String Here %s and Here %s”,”La tua Stringa Qui %s e Qui %s”

    Hope this helps

  3. how to unset or hide Mage::getSingleton(‘core/session’)->addSuccess(“test”); message. plz ans me.

  4. This is useful information, but does not explain how to retrieve and print the error or warning messages. I want to display the error messages that’s all. Just give me code that I can use.

  5. What if you are running another language store?
    Where would we put the error message template?
    Iis there a function that looks up the error message based on whatever language used?

  6. HI… Am receiving Success message twice in a page. May I know Why and how to make it print once?

  7. Hi there,

    Some of my forms submit via AJAX and reload the page on complete. I have the $this->_initLayoutMessages(‘core/session’); line in my controller when the page loads, but the success/error messages never appear on my form page. They appear throughout the rest of the site – the home page, customer account pages, etc. Any ideas on what I’m missing tat might allow them to appear on the page itself?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.