Messages are not shown
When you are working with sessions, you might have the problem, that your message is not shown.
First add them
We have different sessions:
- core
- checkout
- sales
- customer
- admin
- a lot more.
For each of these sessions, we have four methods to add a message:
Mage::getSingleton('*/session')->addSuccess('Woho! It worked!');
Mage::getSingleton('*/session')->addNotice('Task was executed.');
Mage::getSingleton('*/session')->addWarning('Something went wrong.');
Mage::getSingleton('*/session')->addError('Something went horribly wrong!');
And now get them
Getting the message is easy, get the right session and call getMessages()
:
public function getMessages($clear=false)
Mage::getSingleton('core/session')->getMessages(true)
The parameter decides whether the messages are deleted or not.
The problem
The message block
<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>
loads only the messages of core/session
:
app/code/core/Mage/Core/Block/Messages.php:79
public function _prepareLayout()
{
$this->addMessages(Mage::getSingleton('core/session')->getMessages(true));
parent::_prepareLayout();
}
There are a few places, where other session are loaded, e.g. \Mage_Cms_Helper_Page::_renderPage
:
foreach (array('catalog/session', 'checkout/session', 'customer/session') as $storageType) {
$storage = Mage::getSingleton($storageType);
if ($storage) {
$messageBlock->addStorageType($storageType);
$messageBlock->addMessages($storage->getMessages(true));
}
}
But unfortunately not on the product view.
The solution
In my last project we already had an observer which makes sure, that the customer is redirected after sending a contact form, therefore we used the same method to just rewrite the message to the "correct" session.
$customerSession = Mage::getSingleton('customer/session');
$coreSession = Mage::getSingleton('core/session');
foreach ($customerSession->getMessages()->getItems() as $message) {
$coreSession->addMessage($message);
}
$customerSession->getMessages(true);
Better(?) alternative
It is possible to just add all the messages as done in \Mage_Cms_Helper_Page::_renderPage
.
Make sure this is done AFTER $action->loadLayoutUpdates()
/ $this->loadLayoutUpdates()