1

I have to make a cron job that once a day creates users from a file on the server. The script works fine, but, the users it generates can't log in (only local for now, not live).

Here is the code:

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

$customer = $customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->setMediqCustomerId($mediqId);
$customer->setEmail($email);
$customer->setFirstname($name);
$customer->setPassword('Password123');
$customer->save();

Any ideas what I'm doing wrong? When trying to log in with user created with this, it says that the password is wrong.

PS I know not to use OM like this. I just wanted to test if I can even make users in code like this.

Any help is more than welcome. Thanks.

1
  • Please check my ans. Commented Sep 14, 2018 at 9:24

3 Answers 3

1

Please try following code to create customer programmatically

<?php

namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerFactory;

    /**
     * @param \Magento\Framework\App\Action\Context      $context
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Customer\Model\CustomerFactory    $customerFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\CustomerFactory $customerFactory
    ) {
        $this->storeManager     = $storeManager;
        $this->customerFactory  = $customerFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        // Get Website ID
        $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();

        // Instantiate object (this is the most important part)
        $customer   = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);

        // Preparing data for new customer
        $customer->setEmail("[email protected]"); 
        $customer->setFirstname("First Name");
        $customer->setLastname("Last name");
        $customer->setPassword("password");

        // Save data
        $customer->save();
        $customer->sendNewAccountEmail();
    }
}
1

Update your code to this and try, I tried this its working fine at my end:

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

$customer = $customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword('Password123');
$customer->save();

I think you are not using LastName which is required when creating customer.

Or try to create customer using Magento 2 REST APIs

    $userData = array("username" => "adminurser", "password" => "password");
    $ch = curl_init("http://domain.com/rest/V1/integration/admin/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch);


    $customerData = [
        'customer' => [
            "email" => "[email protected]",
            "firstname" => "John",
            "lastname" => "Doe",
            "storeId" => 1,
            "websiteId" => 1
        ],
        "password" => "Demo1234"
    ];

    $ch = curl_init("http://domain.com/rest/V1/customers");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

    $result = curl_exec($ch);

    $result = json_decode($result, 1);
    echo '<pre>';print_r($result);
3
  • Tested, still the same. The thing is, that the user is created, but the password seems to be wrong when trying to login. Any ideas what else it could be?
    – Bawsi
    Commented Sep 14, 2018 at 9:08
  • Try to create customer using REST API, I have updated answer please have a look. Commented Sep 14, 2018 at 9:29
  • Figured it out.. The store_id and website_id were both being set to 0, instead of 1. Idk why tho..
    – Bawsi
    Commented Sep 14, 2018 at 10:02
-1

Figured it out. The website_id and store_id were both being set to 0 instead of 1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.