Would you like to learn some cool coding stuff in Magento 2? Then creating a customer and adding a new address in Magento 2 is something you should know.
In Magento 2, the customer account is created from the frontend via sign-up. Or else, it can be created from the admin panel. Creating multiple customers with different addresses and segregating them into different groups is not an easy thing. The smart way to do it is programmatically in Magento 2 and then by doing manually.
All you have to do is create a $customerInfo array and pass it to the block function. That’s it.
Refer to the below code snippet that tells you how to create a new customer and address them programmatically in Magento 2:
Creating New Customer & Customer Address:
First, check if the customer is already existing or does not have an email id. If it exists, then display “ customer already there” or add a new customer and the address of that customer.
<?php
declare(strict_types=1);
// Customer and customer address array
$customerInfo = [
'customer' => [
'firstname' => 'John',
'lastname' => 'Due',
'email' => 'john.due@magespark.com', //customer email id
'password' => 'john123',
'prefix' => 'Mr',
'suffix' => ''
],
'address' => [
'firstname' => 'John',
'lastname' => 'Due',
'prefix' => 'Mr',
'suffix' => '',
'street' => '3148 Doctors Drive',
'city' => 'Los Angeles',
'country_id' => 'US',
'region' => 'California',
'region_id' => '12', // State region id
'postcode' => '90017',
'telephone' => '9876543210',
'save_in_address_book' => 1
]
];
/**
* @var $block MageSparkCreateCustomerBlockCustomerAddress
*/
$block->createCustomer($customerInfo);
For More Information, Visit:- https://www.magespark.com/blog/post/how-to-create-customer-and-add-new-address-programmatically-in-magento-2?utm_source=writeupcafe&utm_medium=article&utm_campaign=august-2022