Building your own Magento modules (extensions) can be a tedious task. Unlike WordPress, Magento requires a lot more knowledge and dedication to get things right. I must say I hate my self more and more every time I mention WordPress and Magento in the same context. I am well aware that those two are completely different systems. Anyhow, the point is that Magento has bunch of “built in mechanism” that you should conform your development to in order for your module to be built in the “right Magento way”. The problem is that the Magento developer documentation is so freaking poor (or better yet, nonexistent) that average developer needs to spend countless hours to achieve the basic functionality.
One of the nicer “under hood features” is the notification system, at least for me
. When I say notification system I think in terms of messages user is presented upon some successful or unsuccessful action in Magento. Like the one shown in the photo below (little yellow-ish box with “Custom notice here” text).
There are few “notification types” in Magento you can utilize:
- error
Mage::getSingleton(‘core/session’)->addError(‘Custom error here’); - warning
Mage::getSingleton(‘core/session’)->addWarning(‘Custom warning here’); - notice
Mage::getSingleton(‘core/session’)->addNotice(‘Custom notice here’); - success
Mage::getSingleton(‘core/session’)->addSuccess(‘Custom success here’);
Below is a screenshot to show these in action.
There is not much point in calling any of these from your view files. Here is an example on how to set some of these trough controller methods like some imaginary sendemailAction().
... public function sendemailAction() { //Fetch submited params $params = $this->getRequest()->getParams(); $mail = new Zend_Mail(); $mail->setBodyText($params['comment']); $mail->setFrom($params['email'], $params['name']); $mail->addTo('somebody_else@example.com', 'Some Recipient'); $mail->setSubject('Test SimpleContact Module for Magento'); try { $mail->send(); } catch(Exception $ex) { Mage::getSingleton('core/session')->addNotice('Custom notice here'); Mage::getSingleton('core/session')->addError('Custom error here'); Mage::getSingleton('core/session')->addSuccess('Custom success here'); Mage::getSingleton('core/session')->addWarning('Custom warning here'); } //Redirect back to index action of (this) activecodeline-simplecontact controller $this->_redirect('activecodeline-simplecontact/'); } ...
Lets ask our self one more question, what part of the code actually output these messages? Well, if you were to open lets say app/design/frontend/default/default/template/page/3columns.phtml file, you will find something like
...
<!-- start center -->
<div id="main" class="col-main">
<!-- start global messages -->
<?php echo $this->getChildHtml('global_messages') ?>
<!-- end global messages -->
<!-- start content -->
<?php echo $this->getChildHtml('content') ?>
<!-- end content -->
</div>
<!-- end center -->
...Notice the $this->getChildHtml(‘global_messages’) section? The getChildHtml method is taking us to layout files, or in this case page.xml file. We need to look for block called global_messages.
And below is the definition of the block itself.
...
<block type="core/messages" name="global_messages" as="global_messages"/>
...Definition of the block tells us one important thing, the block class core/messages or in full class name Mage_Core_Block_Messages. Combined with the Model class Mage_Core_Model_Message we get two out of three main building blocks of notification system. So what is the third and final building block? If you were to take another look at the way we write the message to the notification system you will see we are doing so with help of session storage mechanizm, Mage::getSingleton(‘core/session’)->…. Lets open the Mage_Core_Model_Session_Abstract. This is our third piece of the puzzle for creating a circle around core notification system in Magento.
Take some time to study the methods in previously mentioned three classes. It will help you understand the inner workings of Magento notification system.
Keep in mind the flexibility, where you are not limited to one storage mechanism. Instead using the Mage::getSingleton(‘core/session’) you could use something like Mage::getSingleton(‘myclass/name_here’).
Good example of using diferent class than ‘core/session’ is the Mage::getSingleton(‘catalog/session’)->addNotice(‘Some notice here’). Here we see the use of ‘catalog/session’, full name Mage_Catalog_Model_Session, class.
Close examination of class reveals the constructor method setting up some stuff
... public function __construct() { $this->init('catalog'); } ...
Tracing the init() method tells us that this storage mechanism is merely using different session namespace to store its notification messages. On one side this can be looked as another way of complicating things while on the other side it can be nice way of organizing things.
The thing is that you are very flexible when it comes to outputting those notification messages. For instance, catalog product view page (view.phtml). If you were to open the file app/design/frontend/default/default/template/catalog/product/view.phtml and look around line 41 you will see the code below.
... <div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div> ...
On the photo below you can see the result of such notification beeing triggered. Note that this message is a result of trying to add a product in less quantity then allowed. Not the behavior of notification system when we comment out the $this->_initLayoutMessages(‘checkout/session’) from app/code/core/Mage/Checkout/controllers/CartController.php around line 139.
Below is the above shot with commented line 139 ( //$this->_initLayoutMessages(‘checkout/session’); ).
Hope this was informative. Cheers.



Good post. It’s nice when someone does the research for you
@Chuck Thank you, I appreciate the feedback. Took me few cups od coffe and few hours to get it on “paper”
Cheers.
A good post indeed. It’s nice to see how an experienced developer makes sense of the huge, confusing system that is Magento
Great stuff branko! Most of the time when I’m looking for something about Magento, you’re the one with the right solution!