1

I am getting this error while loading customer by mail A customer website ID must be specified when using the website scope.

Here is my code:

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

    $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
    $CustomerModel->setStoreId($storeManager->getStore()->getStoreId());
    //$this->setWebsiteId($storeManager->getStore()->getWebsiteId());
    $CustomerModel->setWebsiteId( $storeManager->getWebsite()->getWebsiteId());
2
  • Where are you running this code? – Sukumar Gorai Oct 8 '18 at 11:18
  • use below one, $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer'); $CustomerModel->setWebsiteId(1); $CustomerModel->loadByEmail($customerEmail); $userId = $CustomerModel->getId(); – suresh-vagvala Oct 8 '18 at 11:21
1

So You can use the below code in this way. I have mentioned both the ways, so use any one to get customer model

public function __construct(
 \Magento\Customer\Model\Customer $modelCustomer,
  \Magento\Store\Model\StoreManagerInterface $storeManager
){
 $this->modelCustomer = $modelCustomer;
 $this->storeManager = $storeManager;
}

public function getCustomer()
   {
      $email = abc@gmail.com;
      $model = $this->modelCustomer->setWebsiteId(null)->loadByEmail($email);

    //or if you want get based on website id then,

      $websiteId =$this->storeManager->getStore()->getWebsiteId();
      $model = $this->modelCustomer->setWebsiteId( $websiteId)->loadByEmail($email);


    return $model;

}

Hope it will help you,Happy Coding!!

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.