Here is how you can change the 'My account' menu link into the customer name (John Doe) for logged in users.
I'm going to use the same system that the checkout module uses to add the cart and the checkout link.
First of all create a new module that will extend the Mage_Customer module. We will call it Custom_Customer.
In app/code/local folder create a folder named 'Custom'.
[UPDATE]
Inside the Custom folder create a folder 'Customer'.
/* Thanks for the comment 'Anonymous' - see first comment*/
[/UPTATE]
In this folder create a subfolder called 'Block' (because we need a new block).
Inside 'Block' folder create a php file named 'Links.php' with this content
<?php class Custom_Customer_Block_Links extends Mage_Core_Block_Template { /** ads my account link to some block */ public function addAccountLink() { $parentBlock = $this->getParentBlock();//this block will have a parent if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Customer')) {//check if the customer module is not disabled $customerSession = Mage::getSingleton('customer/session');//the customer session if( $customerSession->isLoggedIn() ) {//if the customer is logged in get the name $text = $customerSession->getCustomer()->getName(); } else {//if the customer is not logged in print 'My account' $text = Mage::helper('customer')->__('My Account'); } //add the block to the set of links. $parentBlock->addLink($text, Mage::helper('customer')->getAccountUrl(), $text, false, array(), 10, null, ''); } return $this; } }
Now create the config.xml file for the module.
[UPDATE]
inside the Customer folder create a subfolder 'etc' and inside it the config.xml with this content.
/* Thanks for the comment 'Anonymous' - see first comment*/
[/UPTATE]
<?xml version="1.0"?> <config> <modules> <!-- the version of the module--> <Custom_Customer> <version>0.0.1</version> </Custom_Customer> </modules> <!-- Hack to tell magento that our block overrides a core block (that does not exits) --> <global> <blocks> <customer> <rewrite> <links>Custom_Customer_Block_Links</links> </rewrite> </customer> </blocks> </global> </config>Now we have to use out block in the design.
in app/design/frontend/{interface}/{theme}/layout/customer.xml replace this:
<default> <!-- Mage_Customer --> <reference name="top.links"> <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> </reference> </default>with this
<default> <!-- Mage_Customer --> <reference name="top.links"> <!-- THe block we've just created and the method to add the link --> <block type="customer/links" name="customer_links"> <action method="addAccountLink" /> </block> </reference> </default>
Now all we have to do is to tell Magento that we have a new module.
in app/etc/modules create a new file called Custom_Customer.xml (the name is not important) with this content:
<?xml version="1.0"?> <config> <modules> <Custom_Customer> <active>true</active> <codePool>local</codePool> <depends><Mage_Customer /></depends> </Custom_Customer> </modules> </config>
Clear the contents of 'var/cache' and enjoy.
Tested on Magento CE 1.5.1.0
For 1.6.0 you need put folder "Customer" inside folder "Custom" and put block & etc inside this folder "Customer". Then it works for me.
ReplyDeleteThanks for this code! It works with this little fix :)
How the folder should be look:
"Custom/Customer/"
"Custom/Customer/Block/"
"Custom/Customer/etc/"
(tested on 1.6.0)
Thanks for the info 'Anonymous' :). I will update the post with your observations.
ReplyDeleteThank you very much. Work just fine in 1.8.1
ReplyDelete