Magento – How to Override Messages on Product Page

global-messagesmessagesmodule

I'm trying to override the messages block in the magento product page [and every other page actually] I'm trying to extend [and override] the default messages class. So far I have this:

in /app/etc/modules/Bbs_Messages.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Bbs_Messages>
            <active>true</active>
            <codePool>local</codePool>
        </Bbs_Messages>
    </modules>
</config>

in /app/code/local/Bbs/Messages/Core/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Bbs_Messages>
            <version>0.1.0</version>
        </Bbs_Messages>
    </modules>
    <global>
        <blocks>
            <core>
                <rewrite>
                    <messages>Bbs_Messages_Block_Messages</messages>
                </rewrite>
            </core>
        </blocks>
    </global>
</config>

in /app/code/local/Bbs/Messages/Block/Messages.php

<?php
class Bbs_Messages_Block_Messagesextends Mage_Core_Block_Messages
{
    public function getGroupedHtml()
    {
        return '<div data-alert class="alert-box"></div>';
    }
}

Then calling it in my view.phtml normally:

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

All I'm getting is an error:

Fatal error: Call to a member function addMessages() on a non-object in /var/www/vhosts/bigblockstudios.ca/subdomains/autokraft/httpdocs/app/code/core/Mage/Core/Controller/Varien/Action.php on line 644

what did I miss here?

Updated:

Update as per RS's answer & corrected the above & added code to help anyone else who happens along with the same problem.

Didn't want to change it that much, so I left the local.xml override options intact [in case someone else ISN'T using Foundation]:

<reference name="root">

            <block type="core/messages" name="global_messages" as="global_messages">
                <action method="setMessagesFirstLevelTagName"><tagName>div</tagName></action>
                <action method="setMessagesSecondLevelTagName"><tagName>span</tagName></action>
            </block>

            <block type="core/messages" name="messages" as="messages">
                <action method="setMessagesFirstLevelTagName"><tagName>div</tagName></action>
                <action method="setMessagesSecondLevelTagName"><tagName>span</tagName></action>
            </block>

        </reference>

Updated /app/code/local/Bbs/Messages/Block/Messages.php with almost exactly the origainl function:

<?php
class Bbs_Messages_Block_Messages  extends Mage_Core_Block_Messages
{

    public function getCustomMessages($wrap_start,$wrap_end)
    {
        $types = array(
            Mage_Core_Model_Message::ERROR,
            Mage_Core_Model_Message::WARNING,
            Mage_Core_Model_Message::NOTICE,
            Mage_Core_Model_Message::SUCCESS
        );
        foreach ($types as $type) {

            // remap types for foundation output
            $_types = array(
                'success' => 'success',
                'notice' => 'info',
                'warning' => 'warning',
                'error' => 'alert',
            );

            if ( $messages = $this->getMessages($type) ) {

                if ( !$html ) {
                    $html .= '<' . $this->_messagesFirstLevelTagName . ' data-alert class="alert-box '.$_types[$type].'">';
                }

                $html .= '<' . $this->_messagesSecondLevelTagName . ' class="' . $_types[$type] . '-msg">';
                $html .= '<' . $this->_messagesFirstLevelTagName . '>';

                foreach ( $messages as $message ) {
                    $html.= '<' . $this->_messagesSecondLevelTagName . '>';
                    $html.= '<' . $this->_messagesContentWrapperTagName . '>';
                    $html.= ($this->_escapeMessageFlag) ? $this->escapeHtml($message->getText()) : $message->getText();
                    $html.= '</' . $this->_messagesContentWrapperTagName . '>';
                    $html.= '</' . $this->_messagesSecondLevelTagName . '>';
                }
                $html .= '</' . $this->_messagesFirstLevelTagName . '>';
                $html .= '</' . $this->_messagesSecondLevelTagName . '>';
            }
        }

        if ( $html) {
            $html .= '</' . $this->_messagesFirstLevelTagName . '>';

            // wrap it with our custom html
            $html = $wrap_start.$html.$wrap_end;
        }

        return $html;
    }


}

This way, the default magento messages are still available & I can get the custom ones by invoking the getCustomMessages function instead. All that needs to be done is to customize the html output of the above function….

Best Answer

Your class name is not correct

Change

class Bbs_Core_Block_Messages extends Mage_Core_Block_Messages

to

class Bbs_Messages_Block_Messages extends Mage_Core_Block_Messages

In your config.xml also change

<blocks>
     <core>
        <rewrite>
            <messages>
                 Bbs_Core_Block_Messages
            </messages>

To (Please note : the class name and all on one line)

<blocks>
     <core>
        <rewrite>
            <messages>Bbs_Messages_Block_Messages</messages>