14

What is the best way to load Customer by Id? using customer interface or customer factory or another way ?
In most of solutions I found, it is done by directly using objectManager (which should be never be used).

23

It's always a better practice to use service contracts.

In your case I would use \Magento\Customer\Api\CustomerRepositoryInterface :

protected $_customerRepositoryInterface;
public function __construct(
    ....
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    ....
) {
    ....
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
}

Then in your code you can call:

$customerId = 1;
$customer = $this->_customerRepositoryInterface->getById($customerId);
  • not working ... i have keep it in helper/data.php and call this method in phtml file but not working. – Sarfaraj Sipai Jun 21 '18 at 9:05

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.