2

I am using below code to get customer id by email

use Magento\Customer\Model\Session;

protected $_objectManager;
protected $currentCustomer;
protected $_customer; 
protected $_storeManager;    
 public function __construct(
    Session $customerSession,
    \Magento\Customer\Model\CustomerFactory $customer,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Store\Model\StoreManagerInterface $storeManager
)
{        
    $this->session = $customerSession;        
    $this->_customer = $customer;       
    $this->_objectManager = $objectmanager;
    $this->_storeManager = $storeManager; 
}

 public function execute()
{
  $email = 'test@gmail.com';
 $customerModel = $this->_objectManager->create('Magento\Customer\Model\Customer');
        $storeId = $this->getWebsiteId();
        $customerModel->setWebsiteId($storeId);
        $customerModel->loadByEmail($email);
        $userId = $customerModel->getId();
 }
 public function getWebsiteId(){
   return $this->_storeManager->getStore()->getWebsiteId();
}

I am not getting customerid, Please anyone suggest me, how to get customer id by email.

  • Can you please upload your full code ? Or use my below code. – Rohan Hapani Sep 26 '18 at 15:07
5

Use this below code :

protected $_customer;
protected $_storemanager;
public function __construct(
    .....
    \Magento\Customer\Model\CustomerFactory $customer,
    \Magento\Store\Model\StoreManagerInterface $storemanager,
    .....
){
    .....
    $this->_customer = $customer;
    $this->_storemanager = $storemanager;
    ...
}
public function execute() {
    $email = 'test@test.com';
    $websiteID = $this->_storemanager->getStore()->getWebsiteId();
    $customer = $this->_customer->create()->setWebsiteId($websiteID)->loadByEmail($email);
    $customerId = $customer->getId();
}
  • I am also using same code, but not working for me, tell me what is the difference in my code – jafar pinjar Sep 26 '18 at 15:07
  • please check my updated answer – Rohan Hapani Sep 26 '18 at 15:11
  • same again customer id is not returning – jafar pinjar Sep 26 '18 at 15:15
  • please check my updated answer. Remove var and generated folder – Rohan Hapani Sep 26 '18 at 15:18
  • not working i tried that also, Uncaught Error: Call to undefined method Magento\\Customer\\Model\\ResourceModel\\CustomerRepository\\Interceptor::getByEmail() – jafar pinjar Sep 26 '18 at 15:22
0

There is another way you can get Customer id by Customer Email.In this use service contact Class \Magento\Customer\Api\CustomerRepositoryInterface

namespace {NameSpace};


class {YOUR_CLass}
{

    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customerRepository;

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

    /**
     * @return \Magento\Customer\Api\Data\CustomerInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function  getCustomer()
    {

        try{
            $customer = $this->customerRepository->get('amit@gmail.com');
            $customerId = $customer->getId();
        }catch(\Magento\Framework\Exception\NoSuchEntityException $exception){

        }catch(\Magento\Framework\Exception\LocalizedException $exception){

        }
        return $customer;
    }
}

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.