3

I would like to change the welcome message to just show initials. Clearly I can do something in the template, however, I would like to extend/over-ride Mage_Page_Block_Html_Welcome to do this 'the proper way'.

I have tried doing this however I cannot get it to work - I just get a blank line. I am expecting the normal line with 'TEST' written in there too:

Here is my config.xml entry:

<global>
    <blocks>
        <page>
            <rewrite>
                <html_welcome>MyCompany_Mymodule_Page_Block_Html_Welcome</html_welcome>
            </rewrite>
        </page>
    </blocks>

My code, just copied from the original file with a small change for test porpoises:

app/code/local/MyCompany/Mymodule/Block/Page/Html/Welcome.php

<?php class MyCompany_Mymodule_Page_Block_Html_Welcome extends Mage_Page_Block_Html_Welcome {

    /**
     * Get block messsage
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (empty($this->_data['welcome'])) {
            if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
                $this->_data['welcome'] = $this->__('TEST Welcome, %s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getName()));
            } else {
                $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
            }
        }

        return $this->_data['welcome'];
    }

    /**
     * Get tags array for saving cache
     *
     * @return array
     */
    public function getCacheTags()
    {
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->addModelTags(Mage::getSingleton('customer/session')->getCustomer());
        }

        return parent::getCacheTags();
    }
}
5
  • I do something related in my welcome block and it works. What happens if you just $this->_data['welcome'] = "test."; ? Are you trying to see message logged in ?
    – Niloct
    Commented Jul 28, 2014 at 11:23
  • @Niloct - I tried as suggested but I could not get it working in my module - I think there is something else I need to do given the toHtml is a protected function. Is your code in a module and do you extend the original class? Do you have a template change? Commented Jul 28, 2014 at 11:29
  • Yeap, it's similar to your sample code. Have you enabled module in etc/modules ?
    – Niloct
    Commented Jul 28, 2014 at 11:33
  • Thanks for your help - sometimes knowing that it can be made to work is all one needs to know. I actually had my class paths wrong -..._Block_Page_ was what I needed to match my file paths. Got there in the end! Commented Jul 28, 2014 at 11:42
  • Great! I sometimes mess up with path too =)
    – Niloct
    Commented Jul 28, 2014 at 11:44

2 Answers 2

2

No code is needed for this.

Go to System - Configuration - General - Design - Header and voila!

3
  • I would like to do more than change the default welcome message, I would like to add my own custom code to show initials, i.e. based on the customer's name. To do that I can put something ugly in the template to get the customer if they are logged in, to then get their name and then get their initials, however I would like to do this the proper magento way without making changes to the template. However, when I try to override-extend the the core code for the welcome message it does not work for me. I would like to know why that is. Thankyou. Commented Jul 28, 2014 at 11:17
  • You have the wrong approach. I suggest you closing this question or mark it as solved and start a new one with the correct info and your question, because with this one it seems you want to change the default welcome message from Magento and not the one when a customer is logged.
    – mbalparda
    Commented Jul 28, 2014 at 11:52
  • I answered my own question with the 'right approach'. Thankyou. Commented Jul 28, 2014 at 11:53
1

Simple mistake was made here - the class path did not match the file path:

app/code/local/MyCompany/Mymodule/Block/Page/Html/Welcome.php

required the config.xml and the 'extends' file to have MyCompany_Mymodule_Block_Page_Html_Welcome

Thanks to @Niloct I was able to correct this rather than it be something more complicated.

For anyone wanting to pull through just the first name, in app/code/local/MyCompany/Mymodule/Block/Page/Html/Welcome.php:

<?php class MyCompany_Mymodule_Block_Page_Html_Welcome extends Mage_Page_Block_Html_Welcome {
    /**
     * Get block messsage
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (empty($this->_data['welcome'])) {
            if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
                $this->_data['welcome'] = $this->__('Welcome, %s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getFirstname()));
            } else {
                $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
            }
        }

        return $this->_data['welcome'];
    }

}

Then in app/code/local/MyCompany/Mymodule/etc/config.xml:

<global>
    <blocks>
        <page>
            <rewrite>
                <html_welcome>MyCompany_Mymodule_Block_Page_Html_Welcome</html_welcome>
            </rewrite>
        </page>
    </blocks>
    ...

Then everything works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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