57

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. ?

Eran Kampf
  • 8,928
  • 8
  • 49
  • 47

10 Answers10

94

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();
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
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.

Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
10

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();
Francis Kim
  • 4,235
  • 4
  • 36
  • 51
Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55
9

for Email use this code

$email=$this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getEmail());

echo $email;
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
AK.
  • 186
  • 2
  • 1
5
$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

Shorabh
  • 190
  • 2
  • 9
  • 1
    Please add an explaination to your code. Otherwise it's not much worth. – lxg Sep 23 '14 at 12:04
  • 3
    what 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
4

The following way you can access all the information from logged user.

$customer_data=Mage::getSingleton('customer/session')->getCustomer();


echo "<pre>" print_r($customer_data);
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Alexandar
  • 81
  • 2
4

For username is same with some modification:

$user=$this->__('Welcome, %s!', Mage::getSingleton('customer/session')->getCustomer()->getName());
echo $user;
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
2

Simply,

$current_customer = $this->_getSession()->getCustomer();

This returns the customer object, then you can get all the details from this customer object.

Manikandan Arunachalam
  • 1,470
  • 3
  • 17
  • 32
2

This way:

$email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
echo $email;
sra
  • 23,820
  • 7
  • 55
  • 89
Fujy
  • 21
  • 1
-5

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

Ali
  • 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