1

I had created a custom API for customer registration in Magento 2.3. I am checking that the weather the customer is already registered or not with below code.

$email = $this->request->getParam('email');
$firstName = $this->request->getParam('firstname');
$lastName = $this->request->getParam('lastname');
$websiteId = $this->request->getParam('websiteId');

$customerFactory = $this->_customerFactory->getCollection();

/**
 * check whether the email address is already registered or not
 */
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);

/**
 * if email address already registered, return the error message
 * else, create new customer account
 */
if ($customer->getId()) {
    echo 'Customer with email '.$email.' is already registered.';  
} else {
    // code...
}

But I am getting the api error when i run in postman. When I checked the api log report it shows below error.

"Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection\\Interceptor::setWebsiteId() in \/var\/www\/html\/app\/code\/MyModule\/Customapi\/Model\/AppcustomerregistrationManagement.php:187\nStack trace:\n#0 [internal function]: MyModule\\Customapi\\Model\\AppcustomerregistrationManagement->signup()\n#1 \/var\/www\/html\/vendor\/magento\/module-webapi\/Controller\/Rest\/SynchronousRequestProcessor.php(95): call_user_func_array(Array, Array)\n#2 \/var\/www\/html\/vendor\/magento\/module-webapi\/Controller\/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process(Object(Magento\\Framework\\Webapi\\Rest\\Request\\Proxy))\n#3 \/var\/www\/html\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4 \/var\/www\/html\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#5 \/var\/www\/html\/vendor\/magento\/framework\/Intercept' in '\/var\/www\/html\/app\/code\/MyModule\/Customapi\/Model\/AppcustomerregistrationManagement.php' on line 187"

1 Answer 1

3

The \Magento\Customer\Model\ResourceModel\Customer\Collection class has no method setWebsiteId. If you want to filter customer collection by website id you must set website_id as a filter field:

/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $customerCollection */
$customer = $customerCollection->addFilter('website_id', $websiteId)
     ->addFilter('email', $email)
     ->getFirstItem();

Or You can use the repository for that purposes:

Add \Magento\Customer\Api\CustomerRepositoryInterface in the constructor, like this:

/**
 * @var \Magento\Customer\Api\CustomerRepositoryInterface
 */
private $customerRepository;

/**
 * ObtainCustomer constructor.
 *
 * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
 */
public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
    $this->customerRepository = $customerRepository;
}

Use that code inside your method:

try {
    $customerEntity = $this->customerRepository->get(
        $email,
        $websiteId
    );
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    // no customer
} catch (\Magento\Framework\Exception\LocalizedException $e) {
    // no customer
}
1
  • thanks @SiarheyUchukhlebau for looking in to my issue and providing me the correct way, it got me the registered customer data. Thanks alot...!!! Commented Jan 28, 2020 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.