Get customer name from customer id using object manager
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();
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?
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.