0

I would like to have the same welcome message for all users which you type in at the backend Configuration > Design > Header.

The only difference should be that the logged in users should have their name out front.

For example: the not logged in users should see

"Hello welcome to our store!"

and the ones who are logged in should see

"Mr Brown. Hello welcome to our store!"

So I guess I have to change the code on the following file: code > local > Mage > Page > Block > Html > Header.php

I tried the following but it didn't work:

/**
 * Retrieve page welcome message
 *
 * @deprecated after 1.7.0.2
 * @see Mage_Page_Block_Html_Welcome
 * @return mixed
 */
public function getWelcome()
{
    if (empty($this->_data['welcome'])) {
        if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome') & $this->__('%s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getName()));
        } else {
            $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
        }
    }

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

}

I'm not good at coding so it would be great if someone could help me on this. Thanks.

1
  • 2
    Notice the comment in front of the getWelcome function you are editing. This is deprecated after Magento CE 1.7.0.2.
    – MatthijsIJ
    Commented Dec 17, 2015 at 10:45

1 Answer 1

6

Please do not just copy the complete file to app/code/local as it was suggested here multiple times. You will have a hard time once you upgrade your shop as you will have to work through all this files manually.

It's better to create a proper rewrite for this.

Create a new module and update your config.xml with the block rewrite:

<?xml version="1.0"?>
<config>
    <modules>
        <YourName_YourModule>
            <version>0.1.0</version>
        </YourName_YourModule>
    </modules>
    <global>
        <blocks>
            <yourmodule_yourname>
                <class>YourName_YourModule_Block</class>
            </yourmodule_yourname>
            <page>
                <rewrite>
                    <html_welcome>YourName_YourModule_Block_Page_Html_Welcome</html_welcome>
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

Then create a new file app/code/local/YourName/YourModule/Block/Html/Welcome.php which extends from the core class. This will only rewrite the _toHtml method and the rest is still taken from the core class.

<?php

class YourName_YourModule_Block_Page_Html_Welcome extends Mage_Page_Block_Html_Welcome {

   /**
     * Get block message
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (empty($this->_data['welcome'])) {
            if (Mage::isInstalled() && $this->_getSession()->isLoggedIn()) {
                $this->_data['welcome'] = $this->__('%s. Hello welcome to our store!', $this->escapeHtml($this->_getSession()->getCustomer()->getName()));

            } else {
                $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
            }
        }

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

Once the Mage_Page_Block_Html_Welcome is ever updated in a future version of Magento, you will have less trouble and less manual work during upgrade.

In addition to this you need to update the text for the not logged in customers via Configuration > Design > Header > Welcome Text as it was already correctly mentioned here.

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.