Magento 2 Programatically created customer login is not working
-
09-04-2021 - |
سؤال
I have created customer using following links:
- https://meetanshi.com/blog/create-customers-programmatically-in-magento-2/ OR
- How to add a customer programmatically in Magento 2?
The customer is created and I can see it in the customers list using admin account.
But I am unable to login into the storefront using the provided credentials which I used during programatically creation process. When I try to login, I get following error:
The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.
I have a clue when I checked the database, but I could not find the actual issue. The clue is for the customer entry in customer_entity
table:
store_id
&website_id
is 0 while normal customers have non-zero value.created_in
column value is "Admin" while customer has value "Default store view".rp_token
andrp_token
columns contains NULL value while normal customer have some values.
I am unable to trace the exact issue. And suggest what should be the solution..
Thanks in advance!
المحلول
I think the problem is the store_id
, website_id
being zero.
At least in Magento 1, a customer associated to the admin website could not login in the frontend. It was there just for creating orders associated to him/her in the backend.
I wrote a few more words about it (but for magento 1) in here: Customer Associated with the Admin Store
But you can simply test if this is the case by changing the values of store_id
and website_id
to some valid values.
Bonus: rp_token
is used for requesting a new password using the "Forgot my password" link. so it's normal to be null for newly created customers.
نصائح أخرى
You should not use object manager. You can now use the account management api to create customers very easily.
Try this way:
<?php
/**
* @category Magento 2
* @author Mattia Kozianowski
*/
declare(strict_types=1);
namespace Vendor\Module;
use Magento\Customer\Api\Data\CustomerInterface;
/**
* Class CreateCustomer
*
* @package Vendor\Module\Controller\Account
*/
class CreateCustomer
{
/**
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
*/
protected $customer;
/**
* @var \Magento\Customer\Api\AccountManagementInterface
*/
protected $accountManagement;
/**
* CreateCustomer constructor.
*
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customer
*/
public function __construct(
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customer
) {
$this->customer = $customer;
$this->accountManagement = $accountManagement;
}
/**
* @return \Magento\Customer\Api\Data\CustomerInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createCustomer(): CustomerInterface
{
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->customer->create();
$customer
->setFirstname('name')
->setLastname('lastname')
->setEmail('email@test.com')
->setStoreId(1)
/*SET ALL YOUR DATA HERE*/
;
$password = 'password123';
return $this->accountManagement->createAccount($customer, $password);
}
}