Jun 19, 2011
kalpesh

Magento: Get customer details : customer id, name, email

Magento get the logged in customer details like customer id, name, email, etc. Check if customer is already logged in or not, if they are logged in then get all the data using customer session object and use it as per the requirement.

1
2
3
4
5
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();
    Mage::log($customerData);
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Array
(
    [entity_id] => 1
    [entity_type_id] => 1
    [attribute_set_id] => 0
    [website_id] => 1
    [email] => john.doe@example.com
    [group_id] => 1
    [increment_id] => 000000001
    [store_id] => 1
    [created_at] => 2007-08-30 23:23:13
    [updated_at] => 2008-08-08 12:28:24
    [is_active] => 1
    [firstname] => John
    [lastname] => Doe
    [password_hash] => 204948a4020ed1d0e4238db2277d5:eg
    [prefix] => 
    [middlename] => 
    [suffix] => 
    [taxvat] => 
    [default_billing] => 274
    [default_shipping] => 274
)

Getting customer details from the $customerData object:

1
2
3
4
5
$customerID = $customerData->getId(); //entity_id can be used as id
$name = $customerData->getFirstname() . ' ' . $customerData->getLastname();
$isActive = $customerData->getIsActive();
$email = $customerData->getEmail();
$incrId = $customerData->getIncrementId();

Similarly, we can get lastname, email, etc. ( [website_id] = getWebsiteId() )

26 Comments

  • mmm, I’m not John Doe..

  • $customer = Mage::getModel(‘customer/customer’)->load(1)->getData();

    this is NOT the logged customer, but the customer with the entity_id=1!!!

  • hi

    how can i get the details about abandon cart details…pls help me

    • LOL these useless, lazy questions ‘I want his…pls help me’. As if anyone will ever answer a question phrased like this. No trying anything beforehand, just a lazy ‘I wanna do this’. In the time taken to spam 40 sites in the hope of getting an answer, anand could have answered it himself.

  • i created module like customer in backend.

    Everything will be perfect but now in front end it displays an error message like this:

    Fatal error: Call to a member function getBackend() on a non-object in D:\wamp\www\mysite\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816

    How can i solve this?

    Give me a solution.

    • Re-index and clear cache and try. If it still don’t solve then Mage::log attribute name on line 816 of the said file as a first step.

  • Had to write it like this on a Magento 1.7.2:

    if (Mage::getSingleton(‘customer/session’)->isLoggedIn()) {
    $customer = Mage::getSingleton(‘customer/session’)->getCustomer();
    $customerData = Mage::getModel(‘customer/customer’)->load($customer->getId())->getData();
    print_r($customerData);
    }

    • Hey,

      I know this is a late response but
      where are you inputting this code?

  • Hello every one
    I am new in magento .Actually I want to integrate magento with sugar crm
    I fallow this guide http://www.magentocommerce.com/wiki/3_-_store_setup_and_management/magento-sugarcrm_integration_guide

    but I am unable to get the customer first name, last name and email through this code.

    When a customer create a account a leads create in CRM but with empty field.
    How can I get the customer first name last name and email .

    Thanks allot in advance

  • Hi Kalpesh i want to remove email validation on (add new customer) page in backend how can i remove please help me Thanks in advance

    • hi to remove email validation simple remove class from Email input box.

  • hi Kalpesh Mehta,

    I am learning customization magento in php.
    Here you are getting Customer details this is working for me…. and we are waiting for more posts about magento.

    One more thing is
    If I want to create my own function(this function will insert my own database)? Where I Create?
    this function where I can call in register.phtml?
    Could you tell me the where insert,update,delete and read functions(built in functions) is there in magento?

    thanks in advance……….

  • “i´m a newbie but i´m trying something complicated (and completely unrelated to your blogpost) or do explain basic basics, i´m too stupid to google for a tutorial ! please hellllp me ! ”

    my sweet lord. how do you put up with these suckers ?

    • @garak: India has created 1 billion of them! No wonder the place is stuck permanently in the 3rd world.

  • Hey,

    Have you tried your code ? Because it will NEVER work. “isLoggedIn” will always return a boolean (so true and false) and never going to return the “customer” entity.

    Michael gave the right way.

    • Agreed! I think I updated this post before a year or so and it seems I had not tested the code that time. I will change it to correct one, thanks!

  • i want to make a contact form page where customer can enter there name email and comment and this all will be show in the backend of magento. so any one can help me in this problem

  • Hi

    I am trying to submit a form from homepage and category list page that will take customer address and other required fields as input. then i am registering a customer with this data and also adding the product to the cart. Now i want to redirect to onepage checkout and wants customer to be logged in on that onepage checkout page programatically.

    All things are achieved excluding the customer information on checkout page. Please help me with this.

  • Hi,

    I have upgrade an magento site all things are working fine, but coupon code is not working for the previous customers. If we apply coupon through the customer accounts those were created after upgradation, its working fine.

    While I trying to find the bug, cause the issue in app/code/core/Mage/Checkout/controllers/CartController.php >> couponPostAction()


    $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : ”)
    ->collectTotals()
    ->save();

    not saving the coupon code for the old customers .

    Please reply asap . I am stucking in this from last 3 days.

    Thanks

    • May be try re-indexing all indices and see if that works?

  • I am learning customization magento in php.
    Here you are getting Customer details this is working for me…. and we are waiting for more posts about magento.

    One more thing is
    If I want to create my own function(this function will insert my own database)? Where I Create?
    this function where I can call in register.phtml?
    Could you tell me the where insert,update,delete and read functions(built in functions) is there in magento?

    My requirement is:
    I created a new page in magento.Example:www.example.com/newpage
    This is working.But i need a form with some fields and upload file option in the new page.
    After submtting the form by user ,send system generated mail to me with user information and downloadable link for uploaded file.

    Could you tell me the way how to implement this requirement.
    thanks in advance……….

  • Hi,
    Is it possible to get Guest user’s information via Cookie in Magento?

    • What info you want to get from Cookie? You should only get info from session wherever possible as Cookies are stored in user’s browser and can be easily manipulated.

  • If i have to printe ZIPCODE of the customer then which ID i have to use…

    as this code is not printing anything

    $customerData->getPincode();

  • i want to get customer addreess , city , country , phone number etc in magento 2.2

  • how to send sms when we create customer invoice in magento 1.9,i just want to get current order id (#1000000155 like )not entity id (155). ,from that i can get the telephone number and send the number.can you guide us and where i have to put the code. kindly suggest asap

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications