Friday, March 14, 2014

How to get Logged in Customer's Information in Magento 1.8

This is quite simple. When a user is logged in, his/her details are stored in session. so, we need to get the Session Object and then extract information out of it. Check the code below ..

<?php
 // Get the Customer Information from Session
 $customer = Mage::getSingleton('customer/session')->getCustomer();
   
 // Print Details
 echo "<br>First Name : ". $customer->getFirstname() ;
 echo "<br>Middle Name : ". $customer->getMiddlename() ;
 echo "<br>Last Name : ". $customer->getLastname();
 echo "<br>Name : ". $customer->getName() ;
 echo "<br>Email : ". $customer->getEmail();
 echo "<br>DOB : ". $customer->getDob();

 // Gender : 123 for Male, 124 for female
 echo "<br>Gender : ". $customer->getGender();     
 echo "<br>Tax/VAT : ". $customer->getTaxvat();

 // Group ID : 1- General, 2 - Wholesale 3 - Retailer 4 - QAAA
 echo "<br>Group ID : ". $customer->getGroupId();  
 echo "<br>Store Name : ". $customer->getCreatedIn();

 // Is Active : 1 for TRUE
 echo "<br>Is Active : ". $customer->getIsActive(); 
   
 /// Whether Subscribed to any Newsletter
 /// 1 for TRUE
 $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customer->getEmail()); 

 echo "<br>Is Subscribed to NewsLetter : " . $subscriber->isSubscribed();
   
 /// Extract Customer's DEFAULT Billing Address
 $customerAddressId = $customer->getDefaultBilling();
   
 if ($customerAddressId)
 {
   $address = Mage::getModel('customer/address')->load($customerAddressId);
   $cust_data = $address->getData();
   echo "<br>Billing First Name : " . $cust_data['firstname'];
   echo "<br>Billing Last Name : " . $cust_data['lastname'];
   echo "<br>Billing Company : " . $cust_data['company'];
   echo "<br>Billing Telephone : " . $cust_data['telephone'];
   echo "<br>Billing Fax : " . $cust_data['fax'];
   echo "<br>Billing Street : " . $cust_data['street'];
   echo "<br>Billing City : " . $cust_data['city'];
   echo "<br>Billing Region : " . $cust_data['region'];
   echo "<br>Billing Country : " . $cust_data['country_id'];
   echo "<br>Billing Zip : " . $cust_data['postcode'];
 }
   
 /// Extract Customer's DEFAULT Shipping Address
 $customerAddressId = $customer->getDefaultShipping();
 if ($customerAddressId)
 {
   $address = Mage::getModel('customer/address')->load($customerAddressId);
   $cust_data = $address->getData();
   echo "<br>Shipping First Name : " . $cust_data['firstname'];
   echo "<br>Shipping Last Name : " . $cust_data['lastname'];
   echo "<br>Shipping Company : " . $cust_data['company'];
   echo "<br>Shipping Telephone : " . $cust_data['telephone'];
   echo "<br>Shipping Fax : " . $cust_data['fax'];
   echo "<br>Shipping Street : " . $cust_data['street'];
   echo "<br>Shipping City : " . $cust_data['city'];
   echo "<br>Shipping Region : " . $cust_data['region'];
   echo "<br>Shipping Country : " . $cust_data['country_id'];
   echo "<br>Shipping Zip : " . $cust_data['postcode'];
 }
?>

To check whether the customer was subscribed to any newsletter, we took help of the following function : 
Mage::getModel('newsletter/subscriber')->loadByEmail()

For further notes on Magento, check other articles :
Magento Notes I, Magento Notes II

1 comment:

Serdar Işıkber said...

nice job, what if customer is guest costumer, how can get guest customer's informations?