Magento – Ajax Request Session Messages

ajaxmessagessession

I have implemented the EasyAjax extension by Vladimir Fishchenko, which is an amazing extension that allows you to make ANY Magento request via Ajax. One problem I am having relates to session messages being stored on Ajax requests.

After executing an Ajax request, like adding a product to the cart for example, session messages that were set during that request appear on subsequent pages that implement the global_messages block. Typically since the page would refresh after adding to the cart, the session messages would be displayed as expected, however because this request is made with Ajax, the session messages are stored but not displayed until the user navigates to another page (by which time the message is a page late).

Is there a way to prevent messages from being stored in the session if the request is Ajax? I can check if a request is Ajax using Mage::getSingleton('easyAjax/core')->isEasyAjax(), but I am unsure of where to implement this.

Best Answer

Magento uses \Mage_Core_Model_Session_Abstract::addMessage to store all the messages.

All messages end up in \Mage_Core_Model_Message_Collection::$_messages.

To clear this container, just call \Mage_Core_Model_Message_Collection::clear. The problem here is, that all the session core/session, customer/session, etc. have their own containers, therefore to delete the messages, you need to clear them all.

But I think it should be enough to get rid of the core/session messages, which are set here: \Mage_Core_Block_Messages::_prepareLayout because these are the messages which are shown in global message block (at least I think so)

Related Topic