I am ajaxifying my magento store, and pretty much everything is going swimmingly, except for one thing:
Problem: I seem to be unable to retrieve and display the messages block in the responses to AJAX requests.
Explanation: I am talking about the red (or green, when it is a success message) bar that appears to the user after trying something that didn't work (e.g. adding more items to the cart than the stock allows). When responding to certain ajax requests that generate errors, I want to display the messages' mark up through a pretty much empty template, which is used to render the response for this ajax request. When no error occurs, a different appropriate response is rendered.
Things I've tried: Here's a few lines of PHP code which I've tried to use:
$_messages = Mage::getSingleton("core/session")->getMessages();
echo $this->getLayout()->createBlock("core/messages")->setMessages($_messages)->getGroupedHtml();
echo $this->getMessagesBlock()->getGroupedHtml();
echo Mage::app()->getLayout()->getMessagesBlock()->
setMessages(Mage::getSingleton('customer/session')->getMessages(true))->getGroupedHtml();
Mage::log
ging the data shows empty message collections.
Here is the layout XML (I'm using $this->loadLayout('ajax_msg_handle');
from the controller):.
<ajax_msg_handle>
<block type="core/template" name="error.root" output="toHtml" template="page/html/ajax-messages.phtml">
<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>
</block>
</ajax_msg_handle>
Also a point of detail, I've considered the following:
Most actions, like the cart's "delete", "edit" and the product page's "add to cart", first redirect to a different place, so a second request is made, which shows the error. Maybe these messages are never displayed right away, but only when responding to the request after the one generating the error. So I've tried to follow this possible convention by redirecting to an action that displays these messages, but this didn't work either.
If anyone can tell me how to get those messages to appear, it'll make my day.