Magento 1.8 – Printing Session Messages

errormagento-1.8messagesnewslettersession

I am using magento 1.8.
I have custom theme. I am unable to get newsletter error/success messages when customer submit their email-id for subscription.

I have done many changes to get and print messages. But it won't work.

I am getting messages by below code

$messages = Mage::getSingleton('core/session')->getMessages(true);
    echo "<pre>"; var_dump($messages);

object(Mage_Core_Model_Message_Collection)#281 (2) {
  ["_messages":protected]=>
  array(0) {
  }
  ["_lastAddedMessage":protected]=>
  object(Mage_Core_Model_Message_Error)#120 (6) {
    ["_type":protected]=>
    string(5) "error"
    ["_code":protected]=>
    string(102) "Il y a eu un problème lors de l'inscription : This email address is already assigned to another user."
    ["_class":protected]=>
    string(0) ""
    ["_method":protected]=>
    string(0) ""
    ["_identifier":protected]=>
    NULL
    ["_isSticky":protected]=>
    bool(false)
  }
}

But I am unable to print it.
I have tried below code.

   foreach($messages->getItems() as $message) {
        echo $message->getText();
}

Even I am not getting any thing in forloop.
Help me,, Thanks in advance:)

Best Answer

You can print the messages in a template like this:

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

But there is a catch.
For example the newsletter subscription message is added to Mage::getSingleton('core/session').
So in order to see te core/session messages you need to add this in the controller that renders the template:

$this->_initLayoutMessages('core/session');

after calling

$this->loadLayout();
Related Topic