Magento2:按客户ID加载客户的最佳方法


Answers:


24

使用服务合同始终是更好的做法。

在您的情况下,我将使用\Magento\Customer\Api\CustomerRepositoryInterface

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

然后在您的代码中可以调用:

$customerId = 1;
$customer = $this->_customerRepositoryInterface->getById($customerId);

不起作用...我将其保留在helper / data.php中,并在phtml文件中调用此方法,但不起作用。
Sarfaraj Sipai
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.