0

In the login page, I'm using the username instead of email. Fetching the email is not working and shows the empty page. Can please anyone help me.

echo $username = $login['username']; 
                    $this->searchCriteriaBuilder->addFilter('r_user_dea', $username, 'like');                   

                    $searchCriteria = $this->searchCriteriaBuilder->create();
                    $list = $this->customerRepository->getList($searchCriteria);


                    if ($list->getTotalCount() > 0) {
                        foreach ($list->getItems() as $item) { 

                            echo $email = $item->getEmail(); exit; 
                            //$login['username'] = $email; 


                        }
                    }

$this->searchCriteriaBuilder->addFilter('r_user_dea', $username, 'like');

in this line, r_user_dea is my custom attribute.

  • What is the $list->getTotalCount() result? – PY Yick Apr 12 '18 at 7:50
  • get the count of list – venkata prasad Apr 12 '18 at 8:18
  • @venkataprasad Please try echo $email = $item->getData('email'); – Nalin Savaliya Apr 12 '18 at 8:48
  • not working $userDea = $this->searchCriteriaBuilder->addFilter('r_user_dea', $username, 'like'); this syntax correct right @NalinSavaliya – venkata prasad Apr 12 '18 at 8:56
0

Try to use this code :

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Customer\Model\Session $customerSession,
    .....

){
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->customerSession = $customerSession;
    ...
}
$customerSession = $this->customerSession->create();
if($customerSession->isLoggedIn()){
    $email_id = "";
    $customer = $this->_customerRepositoryInterface->getById($customerSession->getData('customer_id'));
    if($customer->getFirstname() == "Your user name which you compare")
    {
        $email_id = $customer->getEmail();
    }
}
  • This code is not working. I need at the time of login based on the username get the email address – venkata prasad Apr 12 '18 at 10:05
  • Try to use my updated answer. It will display email id only when customer is logged in. – Rohan Hapani Apr 12 '18 at 10:43
0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
                $collection = $customerObj->addAttributeToSelect('*')
                              ->addAttributeToFilter('r_user_dea',$login['username'])
                              ->load();

               $cData=$collection->getData();

               $login['username'] = '';
                foreach($c_data as $data){
                     $email = $data['email'];
                }
                $login['username'] = $email;
  • Do not use ObjectManager Direct. It defeats the purpose of dependency injection. – Rohan Hapani Apr 12 '18 at 13:01
  • Can you please tell me what issue occured in my answer? Because, I tested it and it's working. – Rohan Hapani Apr 12 '18 at 13:02
  • It's also decrease speed of your webpage. It's not coding standard. – Rohan Hapani Apr 12 '18 at 13:05
  • Thanks for helping. I changed the instead of email to a username so once user enter the username then based on that get the collection. In your code is getting the email after logged in. – venkata prasad Apr 12 '18 at 15:36
  • You posted comment like " I need at the time of login based on the username get the email address". So, I updated my answer. – Rohan Hapani Apr 12 '18 at 18:38

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.