If you want to create customer using programming in Magento 2, then use the following code.
1. Create savecustomer.php file at magento 2 root folder
2. Put below code in the savecustomer.php file
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<?php use \Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstraps = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstraps->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $websiteId = $storeManager->getStore()->getWebsiteId(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Model\AccountManagement; $customer = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create(); $customer->setWebsiteId($websiteId); if ($customer->loadByEmail('stevealbini820@gmail.com')->getId()) { echo '<br>'; echo 'Customer with email '.$email.' is already registered.'; } else { try { // prepare customer data $customer->setEmail('stevealbini820@gmail.com'); $customer->setFirstname('firstName'); $customer->setLastname('lastName'); // set null to auto-generate password $customer->setPassword('password'); $customer->setForceConfirmed(true); // save data $customer->save(); // save customer address // this is optional // you can skip saving customer address while creating the customer $customerAddress = $objectManager->get('\Magento\Customer\Model\AddressFactory')->create(); $customerAddress->setCustomerId($customer->getId()) ->setFirstname('steve') ->setLastname('albini') ->setCountryId('US') ->setRegionId('12') ->setRegion('Florida') ->setPostcode('11003') ->setCity('Miami') ->setTelephone('111-222-4444') ->setFax('111-222-4444') ->setCompany('MageMonkeys') ->setStreet(array( '0' => 'Your Customer Address 1', // compulsory '1' => 'Your Customer Address 2' // optional )) ->setIsDefaultBilling('1') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); try { // save customer address $customerAddress->save(); } catch (Exception $e) { echo '<br>Cannot save customer address.'; } // send welcome email to the customer $customer->sendNewAccountEmail(); try { $accountManagementInterface = $objectManager->get('\Magento\Customer\Api\AccountManagementInterface')->create(); $accountManagementInterface->initiatePasswordReset($email,AccountManagement::EMAIL_RESET,$customer->getWebsiteId()); } catch (NoSuchEntityException $e) { // Do nothing, we don't want anyone to use this action to determine which email accounts are registered. } catch (\Exception $exception) { echo '<br>Cannot send reset link email.'; } } catch (Exception $e) { echo '<br>'; echo $e->getMessage(); } } ?> |
Run this file on browser to create customer. Hope this article is useful.
[crayon-63d3d82567612400025499/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.