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.

2
  • 1
    did you check your customerid is right or not? if invalid then throws error. Sep 22 '17 at 7:23
  • Note a good thing to use the object manager. Its not a good practice and Magento advices to not use it. Sep 22 '17 at 9:04
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();
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?

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.

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();
    }
}
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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.