3

I have a customer ID and I want to get first name and last name of that customer. Any solution?

7

The Best idea to use repository interface for getting customer data Magento 2.2..

Magento\Customer\Api\CustomerRepositoryInterface

Use function getById($customerId)

Inject that class on __construct function then use this class of getting customer object

public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository 
){
  $this->customerRepository = $customerRepository;
}
public function getCustomerDetails()
{
  $customer = $this->customerRepository->getById($customerId);
  if($customer->getId()){
      echo $customer->getFirstname();
      echo $customer->getLastname();

  }
}

You want to use Object Manager then this

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$customer = $customerRepository->getById({CUSTOMER_ID});
if($customer->getId()){
    echo $customer->getFirstname();
    echo $customer->getLastname();

}
1

Use 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();
$customerLastName = $customerObj->getLastName();

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: It is not recommended to use objectManager directly.

1

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 Name:".$customer->getFirstName();
        echo "Customer Name:".$customer->getLastName();
    }

}

Hope, This will help you some extend.

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.