-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserImport.php
57 lines (44 loc) · 1.54 KB
/
UserImport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
// Global vars. Update to suit your needs
$CSVFile ='SampleImport.csv';
$LoginPassword = 'Change123Me!';
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$UserFactory = $objectManager->get('\Magento\User\Model\UserFactory');
$csvFile = fopen($CSVFile, 'r');
while (($line = fgetcsv($csvFile)) !== FALSE) {
// Array structure from csv
// 0 = Username
// 1 = Firstname
// 2 = Lastname
// 3 = Email
// 4 = Role
$Username = (string) $line[0];
$FirstName = (string) $line[1];
$LastName = (string) $line[2];
$Email = (string) $line[3];
$Role = (int) $line[4];
try{
$adminInfo = [
'username' => $Username,
'firstname' => $FirstName,
'lastname' => $LastName,
'email' => $Email,
'password' => $LoginPassword,
'interface_locale' => 'en_US',
'is_active' => 1
];
$userModel = $UserFactory->create();
$userModel->setData($adminInfo);
$userModel->setRoleId($Role);
$userModel->save();
} catch (\Exception $ex) {
echo $ex->getMessage();
echo "\n";
}
echo "$Username was sucessfully created! \n";
}
fclose($csvFile);
?>