I am developing my magento store and almost everything is going smoothly, with the exception of one:
Problem:
It seems I cannot receive and display a block of messages in responses to AJAX requests.
Explanation:
I am talking about red (or green when this message is successful) that appears to the user after trying something that does not work (for example, adding more items to the basket than the stock allows). Responding to specific ajax requests that generate errors, I want to display the message mark through a rather empty template that is used to render the response for this ajax request. When an error does not occur, another corresponding response is visualized.
What I tried:
Here are some lines of PHP code that I 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 data shows empty message collections.
Here is the XML layout (I use $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 in detail, I reviewed the following:
Most of the actions, such as the cart “delete”, “edit” and the product page “add to cart”, first redirect to another place, so the second request is executed, which shows an error. Perhaps these messages are never displayed immediately, but only when responding to a request after it generates an error. So I tried to follow this possible convention by redirecting to an action that displays these messages, but that didn't work either.
If someone tells me how to get these messages, it will make my day.