cancel
Showing results for 
Search instead for 
Did you mean: 

Get user by email or return null in cron

Get user by email or return null in cron

How can i get user by his email, or return null if not found? I need to do this in cron.

This code works

$CustomerModel = $objectManager->create('\Magento\Customer\Api\CustomerRepositoryInterface');

try {                            
$customerExists
= $CustomerModel->get($email); } catch (Exception $e) {
$customerExists
= false; }

But once I run it via cron, it returns an error that email is already in use, even when in try/catch block.

Any ideas?

6 REPLIES 6

Re: Get user by email or return null in cron

Hello,

 

basically, when you run cron we need to check which store is used.

 

Let's an example you have a multiple site 

 

customer a: in website A, customer exist into A website

 

and default website is B

 

when you run cron, it will try to find into B website and it will find no any customer.

 

Hope it will help you.

 

 

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Get user by email or return null in cron

Hello blaz_p

 

Kindly use below code

$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');


$websiteId = $storeManager->getWebsite()->getWebsiteId();
// Get Store ID
$store = $storeManager->getStore();
$storeId = $store->getStoreId();

$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$customer = $customerFactory->create();
$customer->setWebsiteId($websiteId);

try {
    $customer->loadByEmail($customer_email);// load customer by email address
    $data= $customer->getData();
} catch (Exception $e) {                            
    $customerExists = false;
}

 

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"

 

Re: Get user by email or return null in cron

Hi @blaz_p

Do not use objectmanager directly. Use Factory Method instead of object manager is good practice.

 

protected $_customer;

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

public function execute()
{
$customer = $this->_customer->create();
$customer_details = $customer->setWebsiteId($website_id)->loadByEmail($customerEmail);
if($customer_details){
$customerExists = true;
//print_r($customer_details->getData()); //For print customer data
} else {
$customerExists = false;
}
}

 

If issue solved , Click Kudos & Accept as Solution

Re: Get user by email or return null in cron

May I ask why it is always recommended to not use object manager?

 

Thanks 

Re: Get user by email or return null in cron

Hello @blaz_p

 

please check below link for same

 

https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-...


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Get user by email or return null in cron

You can get Customer info by customer email easily.

You don't need to call Objectmanager direclty, instead use of DI injection,

Refer below blog for get data by email id,

https://www.rakeshjesadiya.com/how-to-get-customer-data-by-customer-email-in-magento-2/

In above blog pass your email id and get response as Customer data.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial