I have customer entity id, how do i get customer attribute value by loading customer entity id, Please provide me a solution
2 Answers
you can use this code to get customer attribute value.
class MyClass
{
protected $_customer;
protected $_customerFactory;
public function __construct(...
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\Customer $customers
)
{
...
$this->_customerFactory = $customerFactory;
$this->_customer = $customers;
}
public function getCustomerCollection() {
return $this->_customer->getCollection()
->addAttributeToSelect("*")
->load();
}
public function getFilteredCustomerCollection() {
return $this->_customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("firstname", array("eq" => "Max"))
-load();
}
}
That depends where you want that information, template, some module etc. you can use below code to get customer object data :
$id = xxx; // customer id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')
->load($id);
$customerFirstName = $customerObj->getFirstName();