Add success or error message using magento session

Error message


Set error message for front page.

$message = $this->__('Got an error');
Mage::getSingleton('core/session')->addError($message);
Set error message for admin page.

$message = $this->__('Got an error');
Mage::getSingleton('adminhtml/session')->addError($message);

Success message


Set success message for front page.

$message = $this->__('Got an error');
Mage::getSingleton('core/session')->addSuccess($message);
Set success message for admin page.

$message = $this->__('Got an error');
Mage::getSingleton('adminhtml/session')->addSuccess($message);
If you are unable to view the error/success message, add the function session_write_close() after setting message. I got such problem while using with redirect method. Nitroware solved this problem.Now the new snippet will look like below,

$message = $this->__('Got an error');
Mage::getSingleton('adminhtml/session')->addError($message);
session_write_close();

Notice Message


By this way we can show notice message by using addNotice().

$message = $this->__('Please note this modification');
Mage::getSingleton('core/session')->addNotice($message);

Comments

  1. Hello Karthik,

    Thanks for the useful article.

    How can I display an error message when the user enters wrong login info (frontend) - when trying to login to the site? (where do I add the error message code)

    Thanks again

    ReplyDelete

Post a Comment

Popular posts from this blog

Send email from xampp localhost

Modify item price in cart after placing order using sales_quote_add_item

Convert long url to short url using tinyurl api