0

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.

1

Try this

<?php

protected $_customerRepository;

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

...
...
...

$email = 'john@example.com';
$customer = $this->_customerRepository->get($email);

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.