I am trying to echo global_messages via JSON on the frontend.

I have been looking around for some examples of how to return global_messages as a JSON string but cannot find anything that has helped with my endeavours.

So far I have been able to extend Mage_Core_Block_Messages with my own Company_Core_Block_Messages class then I have attempted to use JSON encode to when returning the $html but have been unsuccessful:

public function getGroupedHtml()
    $types = array(
    $html = '';
    foreach ($types as $type) {
        if ( $messages = $this->getMessages($type) ) {
            if ( !$html ) {
                $html .= '<' . $this->_messagesFirstLevelTagName . ' class="messages">';
            $html .= $this->_renderTextMessages($this->_getTextMessages($messages), $type);
            $html .= $this->_renderPopupMessages($this->_getPopupMessages($messages));
    if ( $html) {
        $html .= '</' . $this->_messagesFirstLevelTagName . '>';
    return $html;

Inside my custom_messages.phtml I would like to echo the returned string but I am not sure if this is the correct way to do this and would like to ask for some guidance please...

    new Ajax.Updater('<?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>');

Has anyone done something similar before?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.