0

I'm trying to set/ display a template file instead of the default messaging in Magento 1.9 using an Observer when the checkout loads but I cannot get the phtml file to show up and I don't know why??

Here is the event that I am observing:

    <events>
        <controller_action_postdispatch_checkout_cart_index>
            <observers>
                <event_handler>
                    <type>singleton</type>
                    <class>my_module/observer</class>
                    <method>myCustomMethod</method>
                </event_handler>
            </observers>
        </controller_action_postdispatch_checkout_cart_index>
    </events>

Next in the Observer I would like to add a phtml file using layout update xml so I have them defined in my module folder:

<custom_module_template>
    <reference name="content">
        <block type="core/template" name="global_messages" template="custom/module/template.phtml" />
    </reference>
</custom_module_template>

In my Observer I call:

Mage::getSingleton('core/layout')->getUpdate()->addHandle('custom_module_template'); 

And I can see the handler is added but nothing is being show on the page. I'm trying to add this template file in the same area as the global_messages block would be.

Am I using the correct apporach?

0
1

The first thing i noticed in your code is that the block type is not core/messages for the 'global_messages' (reference is app/design/frontend/base/default/layout/page.xml where the 'global_messages' block is defined).

However, if you check the app/code/core/Mage/Core/Block/Messages.php class, you will see that this block doesn't use a template file, it is rendered by default and that the HTML is generated in the getGroupedHtml() method.

So basically, you can change the HTML by creating a rewrite for this class and overwriting the getGroupedHtml() method. Just be aware that this class is also used in the admin side so overwriting it will impact the admin messages as well.


LE: if you are not very keen on rewriting the class I suggest another approach which would impact only the frontend and you might be lucky for it to work from the first try: copy the layout/page.xml file in your own theme and replace the 'global_messages' line with a line referencing a custom block class which inherits the app/code/core/Mage/Core/Block/Messages.php and a custom template file, then in the template file specifically call and print the $this->getGroupedHtml() method result.

OR

Use the same approach as described above with a custom block class, only don't overwrite the entire 'page.xml', instead, reference the 'root' block in a custom layout file, invoke the remove block action, then re-add the global messages but this time with your custom block type and template. Something like this:

<layout>
    <default>
        <reference name="root">
            <remove name="global_messages" />
            <block type="custom_module/custom_messages_block" name="global_messages" template="custom_module/page/global_messages.phtml"/>
        </reference>
    </default>
</layout>
4
  • Thanks for your comment. I am trying to avoid a rewrite hence using an Observer, I only need to set a template where a the global_messages should be. Mar 1 at 16:12
  • I see. Check the update in my answer, maybe it helps. It's not using an observer, tho'. Mar 1 at 16:26
  • Botean I see it. However, I need to set this template in an area where the global messages are shown but without rewriting the messages class. Mar 1 at 18:40
  • Well, you aren't rewriting the messages class (as described in my later edit) :) you are replacing it :) Mar 1 at 19:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.