5

I try to use object manager to retrieve customer first name and last name using customer id like this:

$customer = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface')->getById($customerId);
print_r($customer->getFirstName());

unfortunately, it prints empty string.

Idham Choudry
  • 2,818
  • 3
  • 38
  • 62

5 Answers5

9

Try below code :

$customerID = 10; // your customer-id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')
            ->load($customerID);
$customerFirstName = $customerObj->getFirstname();
Manashvi Birla
  • 8,833
  • 9
  • 27
  • 53
4

By ObjectManager:

$customerID = 1; //Customer ID
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerID);
echo $customer->getFirstname(); //Print Customer First Name
echo $customer->getLastname(); //Print Customer Last Name

By Factory Method:

protected $_customers;

public function __construct(
    ...
    \Magento\Customer\Model\Customer $customers
    ...
) {
    ...
    $this->_customers = $customers;
    ...
}

public function getCustomer($customerId)
{
    //Get customer by customerID
    $customer = $this->_customers->load($customerId);
    echo $customer->getFirstname(); //Print Customer First Name
    echo $customer->getLastname(); //Print Customer Last Name
}

public function getCollection()
{
    //Get customer collection
    return $this->_customers->getCollection();
}

Note: Don't use objectManager instance directly in files more details here: To use or not to use the ObjectManager directly?

Prince Patel
  • 22,708
  • 10
  • 97
  • 119
3

You can achieve like this also:

   <?php  



namespace Mastering\Itdesire\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{

    protected $_customer;

    public function __construct(
        \Magento\Customer\Model\Customer $customer,
        \Magento\Backend\Block\Template\Context $context
    )
    {
        $this->_customer = $customer;
        parent::__construct($context);
    }

     public function getCustomer()
    {
        $customerId = '189'; //You customer ID
        $customer = $this->_customer->getCollection()->addAttributeToFilter('entity_id', array('eq' => $customerId));
        echo "Customer Firstname:".$customer->getFirstname();
        echo "Customer Lastname:".$customer->getLastname();
    }

}

Hope, This will help you some extend.

Pramod Kharade
  • 2,832
  • 1
  • 23
  • 39
2

Try following code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;

try{

    require __DIR__ . '/app/bootstrap.php'; 

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $obj = $bootstrap->getObjectManager();
    $customerRepository = $obj->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);

    $customerId = 1;
    $customer = $customerRepository->getById($customerId);

    if ($customer->getId()) {
        echo "Customer Firstname: " . $customer->getFirstname() . "<br/>";
        echo "Customer Lastname: " . $customer->getLastname();
    }

} catch (Exception $e) {
    echo $e->getMessage();
}

OR

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository 
){
    $this->customerRepository = $customerRepository;
}

public function getCustomerDetails()
{
    $customerId = 1;
    $customer = $this->customerRepository->getById($customerId);
    if ($customer->getId()) {
        echo $customer->getFirstname();
        echo $customer->getLastname();
    }
}
Pratik Oza
  • 3,972
  • 11
  • 17
2

/** * @var \Magento\Customer\Model\Customer $customers */ protected $_customers;

public function __construct(\Magento\Customer\Model\Customer $customers)
{
 $this->_customers = $customers;
}

public function getCustomer($customerId)
{
  //Get customer by customerID
  return $this->_customers->load($customerId);
}
public function getCustomerName(){
   $customer = $this->getCustomer($cutomerID);
   //return $customer->getLastname(); // for last name
   return $customer->getFirstname(); // for first name
}
Birjitsinh Zala
  • 1,167
  • 6
  • 19