0

I want to add a top.links that shows user's name instead of "My Account".

In the old way, I've done by:

  1. Remove the line which add My Account link in customer.xml (usually in default handler)
  2. In page/template/links.phtml I add
$session = Mage::getSingleton('customer/session'); 
if($session->isLoggedIn()) {
    $customer = $session->getCustomer();
    $full_name =  $customer->getName();
    $this->addLink($label = $full_name, $url= $this->getUrl('customer/account'), $title='', $prepare = false, $urlParams = array(),
    $position = 10, $liParams = array('class' => 'my-class'), $aParams = null, $beforeText = '', $afterText = ''); 
}

But now, in new theme, I can't do this way because I use page/template/links.phtml for several kinds of links (account.links, checkout.links, top.links, ...)

So, turn back to customer.xml,

<action method="addLink" translate="label title" module="customer">
      <label>My Account</label>
      <url helper="customer/getAccountUrl"/>
      <title>My Account</title>
      <prepare/>
      <urlParams/>
      <position>10</position>
</action>

How to replace "My Account" to user's name if user logged in? Is there any ways to get this done in customer.xml?

3 Answers 3

2

Goto app/code/core/Mage/Page/Block/Template/Links.php

copy to app/code/local/Mage/Page/Block/Template/Links.php then goto addLink() function goto

$link = new Varien_Object(array(

add below code

if($label=='My Account'):
         $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
         if( $loggedIn){
            $label=Mage::getSingleton('customer/session')->getCustomer()->getName();
         }
        endif;

and My Account depend on xml code <label>My Account</label>

1
  • It's not good to just copy a core file - it will prevent upgrades. One should create his own Links-Block deriving from the core class, configure a RewriteRule/mapping to this new custom class, and then just add new functions that get called from local.xml. That would be a cleaner way than this proposal. Jul 12, 2015 at 22:58
2

Thanks to @Amit Bera's answer and this post I make a better way, reduce condition checking.

  1. Copy app/code/core/Mage/Page/Block/Template/Links.php to app/code/local/Mage/Page/Block/Template/Links.php

  2. Duplicate addLink function (line 85) and rename to addCustomerLink

  3. Modify a bit, result is

public function addCustomerLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
                            $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')
    {

        $customerSession = Mage::getSingleton('customer/session'); //get current customer session
        if($customerSession->isLoggedIn()) { // if customer logged in, get name as label
            $label = $customerSession->getCustomer()->getName();
        }
        else {
            if (is_null($label) || false===$label) { // if customer.xml doesn't set label, get default label instead of returning false.
                $label = Mage::helper('customer')->__('My Account');
            }
            // else get label from customer.xml
        }


        $link = new Varien_Object(array(
            'label'         => $label,
            'url'           => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
            'title'         => $title,
            'li_params'     => $this->_prepareParams($liParams),
            'a_params'      => $this->_prepareParams($aParams),
            'before_text'   => $beforeText,
            'after_text'    => $afterText,
        ));

        $this->_addIntoPosition($link, $position);

        return $this;
    }

Finally, in customer.xml, at <default> handle, change method="addLink" to method="addCustomerLink", result is

 <action method="addCustomerLink" translate="label title" module="customer">
   <label>My Account</label>
   <url helper="customer/getAccountUrl"/>
   <title>My Account</title>
   <prepare/>
   <urlParams/>
   <position>10</position>
</action>
3
  • 1
    GOOD Khanh Tran Dinh to follow Marius Blog
    – Amit Bera
    Jul 2, 2014 at 5:29
  • @AmitBera Thank you for inspiration to found this way. Jul 2, 2014 at 5:32
  • 1
    There is no need to copy the whole core file. Just create a new class deriving from the core class and configure a rewrite to this new class. Then add your addCustomerLink-function and everything works perfectly and is upgradeable. Jul 12, 2015 at 22:59
1

I'm super late here, but for anyone else looking up this question. Another solution is since you say in your new theme all of your link blocks are using the same links.phtml template. Just create a new links block with a different links.phtml

  • Create a new links.phtml template anywhere in your theme

  • Copy paste all of the code from the original links.phtml that makes the links render with classes and all.

  • Add the custom stuff you were doing to add the customer name

  • Then declare a new links block with your new links template

block name="new.account.links" template="path/to/new-links.phtml"

-Add your links inside of it the usual way with

method="addLink"

And you're done!

1
  • Serveral days after that, I thought about a similar solution. Thanks for contributing to this question. Jan 21, 2015 at 11:08

Your Answer

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

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