To creating customer and customer address programmatically in Magento 2 using below code snippet. Just Pass $customerInfo array to a block function .
Given below code checks, Customer is existed or not using email id. If the customer is new, Add a new customer and add given address to the same customer otherwise display already exist customer message.
<?php $customerInfo =[ 'customer' =>[ 'firstname' => 'Rakesh', 'email' => 'rakesh.jesadiya@aaaa.com', //customer email id 'lastname' => 'jesadiya', 'password' => 'admin123', 'prefix' => 'Mr', 'suffix' => '' ], 'address' =>[ 'firstname' => 'Rakesh', 'lastname' => 'Jesadiya', 'prefix' => 'Mr', 'suffix' => '', 'street' => 'Abcd street', 'city' => 'Los Angeles', 'country_id' => 'US', 'region' => 'California', 'region_id' => '12', // State region id 'postcode' => '45454', 'telephone' => '1234512345', 'save_in_address_book' => 1 ] ]; $block->createCustomer($customerInfo); ?>
In Block file, Pass customer required data from above $customerInfo array,
<?php namespace Rbj\CreateCustomer\Block; class CustomerAddress extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Api\Data\AddressInterfaceFactory $dataAddressFactory, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, array $data = [] ) { $this->storeManager = $storeManager; $this->customerFactory = $customerFactory; $this->dataAddressFactory = $dataAddressFactory; $this->addressRepository = $addressRepository; parent::__construct($context, $data); } /** Create customer * Pass customer data as array */ public function createCustomer($data) { $store = $this->storeManager->getStore(); $storeId = $store->getStoreId(); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $customer = $this->customerFactory->create(); $customer->setWebsiteId($websiteId); $customer->loadByEmail($data['customer']['email']);// load customer by email to check if customer is availalbe or not if(!$customer->getId()){ /* create customer */ $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($data['customer']['firstname']) ->setLastname($data['customer']['lastname']) ->setPrefix($data['customer']['prefix']) ->setMobile($data['customer']['mobile']) ->setEmail($data['customer']['email']) ->setPassword($data['customer']['password']); $customer->save(); /* save address as customer */ $address = $this->dataAddressFactory->create(); $address->setFirstname($data['address']['firstname']); $address->setLastname($data['address']['lastname']); $address->setTelephone($data['address']['telephone']); $street[] = $data['address']['street'];//pass street as array $address->setStreet($street); $address->setCity($data['address']['city']); $address->setCountryId($data['address']['country_id']); $address->setPostcode($data['address']['postcode']); $address->setRegionId($data['address']['region_id']); $address->setIsDefaultShipping(1); $address->setIsDefaultBilling(1); $address->setCustomerId($customer->getId()); try { $this->addressRepository->save($address); } catch (\Exception $e) { return __('Error in shipping/billing address.'); } } else { return __('Customer is already exist!'); } } }
Run indexer command to see a customer in the admin panel,
php bin/magento indexer:reindex php bin/magento cache:flush