How to get customer id using email-id without using objectManager in Magento 2.2.5

by wakar Ahamad   Last Updated October 09, 2018 07:09 AM

I have a customer-email on custom controller & want to get customer-id without using objectManager.

how can I get?

Currently, I'm getting using objectManger & here is the controller code:-

    <?php
/**
 * @author      Vladimir Popov
 * @copyright   Copyright © 2017 Vladimir Popov. All rights reserved.
 */

namespace VladimirPopov\WebForms\Controller\Customer;

use Magento\Framework\App\Action\Action;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\RequestInterface;


class Info extends Action
{
    protected $_customerSession;

    protected $_coreRegistry;

    protected $resultPageFactory;

    protected $_formFactory;

    protected $_storeManager;

    protected $_customerFactory;

    protected $_addressFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        \Magento\Customer\Model\SessionFactory $sessionFactory,
        \VladimirPopov\WebForms\Model\FormFactory $formFactory,
        \Magento\Store\Model\StoreManager $storeManager,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
         \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_coreRegistry = $coreRegistry;
        $this->_customerSession = $sessionFactory->create();
        $this->_formFactory = $formFactory;
        $this->_storeManager = $storeManager;
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
        parent::__construct($context);
    }

    /**
     * Retrieve customer session object
     *
     * @return \Magento\Customer\Model\Session
     */
    protected function _getSession()
    {
        return $this->_customerSession;
    }

    /**
     * Check customer authentication
     *
     * @param RequestInterface $request
     * @return \Magento\Framework\App\ResponseInterface
     */


    public function execute()
    {
        $customerEmail=$this->getRequest()->getPost('email_id');
        if(!empty($customerEmail)){
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
        $CustomerModel->setWebsiteId(1); //Here 1 means Store ID**
        $customerInfo=$CustomerModel->loadByEmail($customerEmail);

        if(!empty($customerInfo->getId())){
        $customer = $this->_customerFactory->create()->load($customerInfo->getId());

        $shippingAddressId = $customer->getDefaultShipping();

         $shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);

         $fname=!empty($shippingAddress->getData('firstname'))?$shippingAddress->getData('firstname'):'';
         $lname=!empty($shippingAddress->getData('lastname'))?$shippingAddress->getData('lastname'):'';
         $company=!empty($shippingAddress->getData('company'))?$shippingAddress->getData('company'):'';
         $tel=!empty($shippingAddress->getData('telephone'))?$shippingAddress->getData('telephone'):'';
         $state=!empty($shippingAddress->getData('region'))?$shippingAddress->getData('region'):'';
         $country_id=!empty($shippingAddress->getData('country_id'))?$shippingAddress->getData('country_id'):'';

         $customer_arr=array(
                    'customer_id'=>$customerInfo->getId(),
                    'fname'=>$fname,
                    'lname'=>$lname,
                    'company'=>$company,
                    'tel'=>$tel,
                    'state'=>$state,
                    'country_id'=>$country_id
            );



             }else{
                $customer_arr=array('customer_id'=>'na','customer_not_exist'=>"customer doesn't exist") ;

             }
               echo json_encode($customer_arr); exit;
         }

         // echo "<pre>Tatify " ; print_r($customer_arr) ; exit;
    }
}

Note:- Before hitting this controller, it's not necessary that customer is login in his/her account.

Waiting for early response.



Answers 1


Try this

<?php

protected $_customerRepository;

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

...
...
...

$email = '[email protected]';
$customer = $this->_customerRepository->get($email);
Dinesh Yadav
Dinesh Yadav
October 09, 2018 06:39 AM

Related Questions


How to add customer Id in table magento 2

Updated November 18, 2017 12:09 PM

Only allow 6 digit number as customer password

Updated April 02, 2018 05:09 AM

Magento 2: How to get first and last name of customer?

Updated January 03, 2018 17:09 PM