1
votes

I want to change the position for message(success/error) in magento. I got the code for that.

This one is layout:

<layout>
    <default>
        <reference name="after_body_start">
            <block type="core/template" name="top.messages" template="core/messages/top.phtml" before="-" />
        </reference>
    </default>
</layout>

This one is template file:

<?php $_messageCollection = $this->getMessagesBlock()->getMessageCollection() ?>

<?php if ($_messageCollection->count()): ?>
<div>
<?php
    echo $this->getMessagesBlock()->getGroupedHtml();
    $_messageCollection->clear();
?>
</div>
<?php endif; ?>

This is one of the code used for change the error/success message to the top of the page.

I need to change the design for the messages. Which file contain the messages.phtml? I used this path file app/design/frontend/your_package/your_theme/template/core/.

But its not working. Can any one help to change the design for this.

Thanks

2

2 Answers

5
votes

The html is not being generated in a template file but instead in a block - Mage_Core_Block_Messages

Therefore, if you want to customise this then either:

  1. Rewrite Mage_Core_Block_Messages so you can provide your own version of getGroupedHtml()
  2. Customise your template so that it uses getMessages() instead of getGroupedHtml()

If you are going with method 2 then you could look at core/messages.phtml for inspiration. Your template could look something similar to:

<?php  

$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->getMessagesBlock()->getMessages($type) ) {
        if ( !$html ) {
            $html .= '<ul class="messages">';
        }
        $html .= '<li class="' . $type . '-msg">';
        $html .= '<ul>';

        foreach ( $messages as $message ) {
            $html.= '<li>';
            $html.= $message->getText();
            $html.= '</li>';
        }
        $html .= '</ul>';
        $html .= '</li>';
    }
}
if ( $html) {
    $html .= '</ul>';
}
echo $html;
?>
<?php $_messageCollection = $this->getMessagesBlock()->getMessageCollection()->clear() ?>

not the nicest code for a template though, so you will want to consider re-factoring and moving out some of the logic to a block, which will involve creating your own module.

-1
votes

Prasoft First of What I understand You need to change the position and the design of the Error messages.

I. To change Design: You can edit Css to do that but if you want to change the HTML structure also then refer the file

magento/app/design/frontend/base/default/template/core/messages.phtml

Of course for you need to do the changes in your theme.

II. To change the Position of the Messages you need to refer the file.

magento/app/design/frontend/base/default/template/page/1column.phtml

refer the code.

<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<?php echo $this->getChildHtml('after_body_start') ?>
<div class="wrapper">
   <?php echo $this->getChildHtml('global_notices') ?>
    <div class="page"> 
    <?php echo $this->getChildHtml('header') ?>
    <div class="main-container col1-layout">
        <div class="main">
            <?php echo $this->getChildHtml('breadcrumbs') ?>
            <div class="col-main">
                <?php echo $this->getChildHtml('global_messages') ?>
                <?php echo $this->getChildHtml('content') ?>
            </div>
        </div>
    </div>
    <?php echo $this->getChildHtml('footer') ?>
    <?php echo $this->getChildHtml('before_body_end') ?>
</div>
</div>
<?php echo $this->getAbsoluteFooter() ?>
</body>

As you are talking about the Error messages that always comes under the section global_messages So to change the position re-locate this <?php echo $this->getChildHtml('global_messages') ?> line according to your HTML structure.