1

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(
        Mage_Core_Model_Message::ERROR,
        Mage_Core_Model_Message::WARNING,
        Mage_Core_Model_Message::NOTICE,
        Mage_Core_Model_Message::SUCCESS
    );
    $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...

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

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.