I recently got to a situation where I needed to add a new frontend template for some module and after some action show the error or success message. Not the first time though
. Interesting thing was even if I added this code in the PHTML file.
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
It did not work. Yes, after adding this block it had worked before. In my case, the probable case might be that I had called the module’s template from CMS page. In other case it should work. So, I looked for work around and the following did the trick.
// Getting Messages from Session
$messages=Mage::getSingleton("customer/session")->getMessages();
// Creating Block Mage_Core_Block_Messages
// Setting Message
// echoing the Message's HTML
echo $this->getLayout()->createBlock("core/messages")->setMessages($messages)->getGroupedHtml();
The Error/Success or Notice messages are set on session. So what I did was take those message/s from the session and create a new block, same as what $this->getMessagesBlock() might have called, and set those message to the created block and echoed its HTML.
Clever? or not?
Thanks for the snippet – almost what i needed and brought me on the right track.
Please note that there are different sessions and each can contain messages, so depending on your needs you might need to modify:
$messages=Mage::getSingleton(“customer/session”)->getMessages();
to
$messages=Mage::getSingleton(“core/session”)->getMessages();
or
$messages=Mage::getSingleton(“checkout/session”)->getMessages();
..
(if there is 3 sessions there might be more..
)
For weeks I have suffered this problem and always wondered why… different sessions! of course! Legends!
Don’t forget the Mage::getSingleton(“adminhtml/session”)->getMessages() for the backend.