Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

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

https://www.magespark.com/
Do you like Magespark's articles? Follow on social!

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Warning: array_key_exists() expects parameter 2 to be array, string given in /home2/writeup/public_html/wp-content/plugins/jet-popup/includes/settings.php on line 275

Login

Welcome to WriteUpCafe Community

Join our community to engage with fellow bloggers and increase the visibility of your blog.
Join WriteUpCafe