1

I need to load the customer by using customer repository to check if this customer is exists i used dependency injection in controller but it doesn't work. Can anyone help me? Many thanks

 namespace ThuyNT\SSO\Controller\Account;

class Index extends \Magento\Framework\App\Action\Action
 {

protected $resultPageFactory;


public function __construct(
    Magento\Customer\Api\CustomerRepositoryInterface $customerRepo)
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->customerRepo = $customerRepo;
    parent::__construct($context);
}


public function execute()
{
    try{
        $customerId =1;
          $customer =  $this->customerRepositoryInterface->getById($customerId);
          return $customer;
    }
    catch (\Exception $e) {
        return $this->_messageManager->addError('failed');

}

}

  • please paste your code in question and also let me know what you facing error? – Rutvee Sojitra May 20 at 11:24
  • i updated my code above, thanks for your help – trem jenny May 21 at 2:16
  • Do you already have customer_id? You just need to check id(i.e. customer) already exist or not? – Saphal Jha May 21 at 5:20
  • i used default customer id of Magento2 and i just need to check if this id exist or not – trem jenny May 21 at 6:57
0

Try use:

 public function __construct(
        Magento\Customer\Api\CustomerRepositoryInterface $customerRepo){
        $this->customerRepo = $customerRepo;
    }

And your code check customer exist:

$customer = $this->customerRepo->getById('your customer id');
if($customer){
      //process your code
}
0

Add following in you Constructor:-

     protected $_customer;
    protected $_customerFactory;

public function __construct(...
                            \Magento\Customer\Model\CustomerFactory $customerFactory,
                            \Magento\Customer\Model\Customer $customers
)
{
    ...
    $this->_customerFactory = $customerFactory;
    $this->_customer = $customers;
}


for collection :-

$this->_customer->getCollection()
               ->addAttributeToSelect("*")
               ->load()


For filter collection:-

$this->_customerFactory->create()->getCollection()
                ->addAttributeToSelect("*")
                ->addAttributeToFilter("firstname", array("eq" => "Arun"))
                -load()
0

Inject the following

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

Load by ID


   /**
     * Get customer by Id.
     *
     * @param int $customerId
     *
     * @return \Magento\Customer\Model\Data\Customer
     */
    public function getById($customerId)
    {
        try {
            return $this->customerRepositoryInterface->getById($customerId);
        } catch (\Exception $e) {
            $this->logger->critical($e);
            return false;
        }  
    }     

Load by Email

   /**
     * Get customer by email.
     *
     * @param string $email
     *
     * @return \Magento\Customer\Model\Data\Customer
     */
    public function getByEmail($email)
    {
        try {
            return $this->customerRepositoryInterface->get($email);
        } catch (\Exception $e) {
            $this->logger->critical($e);
            return false;
        }  
    }  

Class to look at

https://github.com/magento/magento2/blob/2.3/app/code/Magento/Customer/Api/CustomerRepositoryInterface.php

  • which class should i put this code? thanks for your help – trem jenny May 21 at 2:18
  • I recommend you create your own helper. Then load helper in controller. – Dominic Xigen May 21 at 15:17

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.