Vladimir
Vladimir

Reputation: 11

Troubles with global messages in Magento

My knoledges in magento not so deep so I had a trouble with the magento global messages output.

I created my module. On the index page of my module (the route is /vouchers) I had a form. So by POST i send data to Controller (the route is the same (/voucehrs)) where I validate data:

if(!isset($post["to_name"]) || !$post['to_name'])
   {
       Mage::getSingleton('customer/session')->addError($this->__('Please enter  "To Name" information'));
       $errors = 1;
   }

...

  if(!$errors)
     {
        Mage::getSingleton('customer/session')->addSuccess($this->__('Your order is successfully saved and now is in process'));
     }

In the template I use <?php echo $this->getMessagesBlock()->getGroupedHtml() ?> but nothing happens, though I dumped Session variable where I can see success message.

Where I was wrong?

Upvotes: 0

Views: 1732

Answers (5)

mikejw
mikejw

Reputation: 183

I found this stack overflow post extremely helpful.

What worked for me was a combination of Alex Ivanov's and Emipro Technologies Pvt. Ltd's answsers, as below (controller code):

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

Upvotes: 0

Ravi Patel
Ravi Patel

Reputation: 5211

Notice
Mage::getSingleton('core/session')->addNotice('Notice message');

enter image description here

Success
Mage::getSingleton('core/session')->addSuccess('Success message');

enter image description here

Error
Mage::getSingleton('core/session')->addError('Error message');

enter image description here

Warning (admin only)
Mage::getSingleton('adminhtml/session')->addWarning('Warning message'); 

enter image description here

Output errors from a controller

public function sendAction()
    {
        try
        {
            $mail = new Zend_Mail();
            $mail->send();
        }
        catch (Exception $e)
        {
            Mage::getSingleton('core/session')->addError('Error sending mail');
        }
        $this->loadLayout();
        $this->renderLayout();
    }

MOre info for session in mageno how to get and set

Upvotes: 2

Alex Ivanov
Alex Ivanov

Reputation: 436

Please check your controller. If you renders the layout, you must include the following code in your action.

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

So your action should be like this :

$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();

It may help you.

Upvotes: 3

Mageworx
Mageworx

Reputation: 922

First, we strongly advise you to install/configure Xdebug - this tool will save you lots of time.

As for the issue, try to use

Mage::getSingleton('customer/session')

instead of

Mage::getSingleton('core/session')

If it doesn't work, you may try:

// after post form
$this->getLayout()->getMessagesBlock()->setMessages(Mage::getSingleton(‘customer/session’)->getMessages(true));

// when display
$this->getLayout()->getMessagesBlock()->getGroupedHtml();

Hope it will help. :)

Upvotes: 0

Can you try core/session instead of customer/session? It may help you.

Upvotes: 0

Related Questions