1

I used a custom script to import customers from a csv, which works as I expected. except few things.

One of them is, user password.

I use below code to import password.

//Read csv


$file = 'import/customer.csv'; 
        $arrResult = array();
        $headers = false;
        $handle = fopen($file, "r");
        if (empty($handle) === false) {
            while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
                if (!$headers) {
                    $headers[] = $data;
                } else {
                    $arrResult[] = $data;
                }
            }
            fclose($handle);
        }

import customer section

foreach( $arrResult as $importCustomer ) {

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

$customer->setEmail($importCustomer[0]); //email
$customer->setWebsiteId($websiteId); //website id
$customer->setStoreId($getDefaultStoreId); //store id 
$customer->setFirstname($importCustomer[4]); //firstname
$customer->setGroupId( (int)$importCustomer[5] ); // group id 
$customer->setLastname($importCustomer[6]); //lastname
$customer->setMiddlename($importCustomer[7]); // middlename
$customer->setPrefix($importCustomer[8]); //prefix
$customer->setSuffix($importCustomer[10]); //suffix
$customer->setUpdatedAt($importCustomer[11]); // updated at
$customer->setPassword($importCustomer[13]); //password

$customer->save();

echo 'Create customer successfully'.$customer->getId()."<br>";

}

I used sample/demo csv file with some sample data. Script runs successfully and I can see that customers are added from magento admin. Usual data, like Name, Email are correct.

Problem occurs when I try to log in as a user in frontend. I cannot log in and when I try to log in, page refreshes and stays in login page. no error messages visible and no errors are loaded into the browser console.

What is the problem here?How can I fix this?

0

Try this code in password set:

$encryptor = $objectManager->get('\Magento\Framework\Encryption\Encryptor');
$passwordHash = $encryptor->getHash($importCustomer[13], true);
$customer->setPassword($passwordHash); //password
3
  • in line 2, $this->_encryptor should be $encryptor right? – OCdave Jun 6 '18 at 11:30
  • yes right i have updated. – user68116 Jun 6 '18 at 11:33
  • I think something was wrong with my csv file. that's why I get the weird page refresh with no error messages. However your answer DID help me. Thanks. Tried to upvote,but since I've no reputation to upvote,it shows me an error. I'll just click on accept. – OCdave Jun 6 '18 at 11:40

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.