Categories
Magento

Magento – Flash / Session Notification Messages

Magento offers an inbuilt flash message class and functions to display success, error, warning and notice messages. You would have seen a message similar to the one below, when submitting the Magento contact form.

Magento Contact Form Submission Success Notification Message

What’s happening there is that the contact form module redirects the visitor back to the Contact Us page by setting a success message in the session class. When the landing page loads, the messages block displays all flash messages that are held in the session.

[php]
<?php
Mage::getSingleton(‘core/session’)->addSuccess(‘Success Message’);
Mage::getSingleton(‘core/session’)->addError(‘Error Message’);
Mage::getSingleton(‘core/session’)->addWarning(‘Warning Message’);
Mage::getSingleton(‘core/session’)->addNotice(‘Notice Message’);
?>
[/php]

In your custom module or anywhere in phtml code, you can add the above code and Magento will display the message on the next page that you load.

10 replies on “Magento – Flash / Session Notification Messages”

Thank you for this clear & concise article – so, how do you display a message on the current page (as opposed to adding a message to the session that is then displayed on the next page, like this does)?
I’m thinking, for example, of displaying a warning saying “This module is in beta”.

Thanks Kathir,

In the end I just output the HTML like this:

This feature is under construction.

Perhaps not the most elegant solution, but turned out to be the easiest by a long shot :)

public function preDispatch()

{

parent::preDispatch();

if(!Mage::getSingleton(‘customer/session’,array(‘name’=>’frontend’))->isLoggedIn()){

$redirectUrl = $this->_getUrl(‘customer/account/login’,$this->getLoginUrlParams());

if($_SERVER[‘HTTP_REFERER’] != $redirectUrl){
Mage::getSingleton(‘core/session’)->addSuccess(‘Please login to access this feature.’);
$this->_redirectError($redirectUrl);

}

}

}

Comments are closed.