We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    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

    <?php
    use MagentoFrameworkAppBootstrap;
    
    require __DIR__ . '/app/bootstrap.php';
    
    $bootstraps = Bootstrap::create(BP, $_SERVER);
    
    $objectManager = $bootstraps->getObjectManager();
    
    $appState = $objectManager->get('MagentoFrameworkAppState');
    
    $appState->setAreaCode('frontend');
    
    $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
    
    $websiteId = $storeManager->getStore()->getWebsiteId();
    
    $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
    
    $connection = $resource->getConnection();
    
    use MagentoCustomerApiAccountManagementInterface;
    
    use MagentoCustomerModelAccountManagement;
    
    $customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->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('MagentoCustomerModelAddressFactory')->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('MagentoCustomerApiAccountManagementInterface')->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.

    field_5bfb909c5ccae

      Get a Free Quote