cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.0 get a customer by id

SOLVED

Magento 2.0 get a customer by id

In Magento 1.x i can use this code to load the customer by id. Mage::getModel('customer/customer')->load($id);


Whats the way to do it in Magento 2.0? I am tring to include the 'customerRepository' Module to my customized module, but its not working, and here is my code:

class Create implements Createcustomer
{
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\Data\CustomerSecureFactory $customerSecureFactory,
\Magento\Customer\Model\CustomerRegistry $customerRegistry,
\Magento\Customer\Model\ResourceModel\AddressRepository $addressRepository,
\Magento\Customer\Model\ResourceModel\Customer $customerResourceModel,
\Magento\Customer\Api\CustomerMetadataInterface $customerMetadata,
\Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory $searchResultsFactory,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
DataObjectHelper $dataObjectHelper,
ImageProcessorInterface $imageProcessor,
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
) {
$this->customerFactory = $customerFactory;
$this->customerSecureFactory = $customerSecureFactory;
$this->customerRegistry = $customerRegistry;
$this->addressRepository = $addressRepository;
$this->customerResourceModel = $customerResourceModel;
$this->customerMetadata = $customerMetadata;
$this->searchResultsFactory = $searchResultsFactory;
$this->customerRepository = $customerRepository;
$this->eventManager = $eventManager;
$this->storeManager = $storeManager;
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
$this->dataObjectHelper = $dataObjectHelper;
$this->imageProcessor = $imageProcessor;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
}
public function create($email, $password,$lastName,$firstName) {

 

$customer = $this->customerFactory->create();
$customer->setWebsiteId(1)
->setEmail($email)
->setFirstname($firstName)
->setLastname($lastName)
->setPassword($password)
->save();

$id = $customer->getId();
$newCus = $this->customerRepository->get($id);


return $newCus;
}

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.0 get a customer by id

I am not promising to answer all programming questions here - I had intended to focus more on strategic product directions and architecture. However I think the problem is you need to use getById($id) instead of get($emailAddress).

View solution in original post

2 REPLIES 2

Re: Magento 2.0 get a customer by id

I am not promising to answer all programming questions here - I had intended to focus more on strategic product directions and architecture. However I think the problem is you need to use getById($id) instead of get($emailAddress).

Re: Magento 2.0 get a customer by id

(Marking as accepted to help spot unanswered questions. Let me know if there is anything further you need.)