I'm customizing the product view page and I need to show the user's name. How do I access the account information of the current user (if he's logged in) to get Name etc. ?
10 Answers
Found under "app/code/core/Mage/Page/Block/Html/Header.php":
public function getWelcome()
{
if (empty($this->_data['welcome'])) {
if (Mage::app()->isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_data['welcome'] = $this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getName());
} else {
$this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
}
}
return $this->_data['welcome'];
}
So it looks like Mage::getSingleton('customer/session')->getCustomer()
will get your current logged in customer ;)
To get the currently logged in admin:
Mage::getSingleton('admin/session')->getUser();

- 8,363
- 9
- 42
- 53
-
2`Mage::app()->isInstalled()` is deprecated, you can use `Mage::isInstalled()` now. – mhyassin Aug 09 '16 at 16:24
Have a look at the helper class: Mage_Customer_Helper_Data
To simply get the customer name, you can write the following code:-
$customerName = Mage::helper('customer')->getCustomerName();
For more information about the customer's entity id, website id, email, etc. you can use getCustomer function. The following code shows what you can get from it:-
echo "<pre>"; print_r(Mage::helper('customer')->getCustomer()->getData()); echo "</pre>";
From the helper class, you can also get information about customer login url, register url, logout url, etc.
From the isLoggedIn function in the helper class, you can also check whether a customer is logged in or not.

- 25,063
- 15
- 119
- 120
You can get current login customer name from session in following way :
$customer = Mage::getSingleton('customer/session')->getCustomer();
This will return the customer details of current login customer.
Now you can get customer name by using getName()
echo $customer->getName();

- 4,235
- 4
- 36
- 51

- 4,404
- 6
- 30
- 55
for Email use this code
$email=$this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getEmail());
echo $email;

- 8,363
- 9
- 42
- 53

- 186
- 2
- 1
$customer = Mage::getSingleton('customer/session')->getCustomer(); $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling(); $address = Mage::getModel('customer/address')->load($customerAddressId); $fullname = $customer->getName(); $firstname = $customer->getFirstname(); $lastname = $customer->getLastname(); $email = $customer->getEmail(); $taxvat = $customer->getTaxvat(); $tele = $customer->getTelephone(); $telephone = $address->getTelephone(); $street = $address->getStreet(); $City = $address->getCity(); $region = $address->getRegion(); $postcode = $address->getPostcode();
Get customer Default Billing address

- 190
- 2
- 9
-
1
-
3what exactly you need explanation to? @shorabh listed all the functions needed to fetch customers daata ... Every single programer should understand this and no non-programers should touch magento code by themselfs. – Miha Trtnik Dec 08 '14 at 09:02
For username is same with some modification:
$user=$this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getName());
echo $user;

- 10,343
- 11
- 51
- 59

- 41
- 1
Simply,
$current_customer = $this->_getSession()->getCustomer();
This returns the customer object, then you can get all the details from this customer object.

- 1,470
- 3
- 17
- 32
I don't know this off the top of my head, but look in the file which shows the user's name, etc in the header of the page after the user has logged in. It might help if you turned on template hints (see this tutorial.
When you find the line such as "Hello <? //code for showing username?>"
, just copy that line and show it where you need to

- 261,656
- 265
- 575
- 769
-
The code showing that is getWelcome() ?> in header.phtml. No idea where it gets the name from... – Eran Kampf Jan 06 '09 at 14:27
-
If you can edit your question and add to it the output of that function, I can write some PHP code for you to extract the username out of it. Magento has gazillions of files, so it can be pretty hard to tell where a particular function is stored – Ali Jan 06 '09 at 16:28
-
Thanks but I can write the code that extracts the name from the welcome string. However I was hoping to be able to read other data other than the name - specifically, a unique user account ID. – Eran Kampf Jan 07 '09 at 09:40