magento2 update customer address programmatically

magento2 update customer address programmatically

In this post we will show you magento2 update customer address programmatically, hear for magento2 update customer address programmatically


By using this code We can add customer to any store and website. once the customer created we can add address to the customer programmatically this following code in magento2. in this given code we register customer after it we pass address of customer and update detail.


we will give you code for implement magento2 update customer address programmatically. we will show you how to update/change customer or user address programmatically. hear we pass customer or user detail like First name, last name, company, postcode and other information. we will show you how to change customer first name, customer middle name, customer last name, customer prefix, customer suffix, customer company, customer street, customer country_id , customer city, customer region_id, customer region, customer postcode , customer country_id, telephone, customer fax

use \Magento\Framework\App\Bootstrap;

$html_bootstrap = Bootstrap::create(BP, $_SERVER);
$set_objectManager = $html_bootstrap->getObjectManager();

$obj_url = \Magento\Framework\App\ObjectManager::getInstance();
$set_storeManager = $obj_url->get('\Magento\Store\Model\StoreManagerInterface');
$get_mediaurl =  $set_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

$get_state = $set_objectManager->get('\Magento\Framework\App\State');

// Customer Factory to Create Customer
$get_customerFactory = $set_objectManager->get('\Magento\Customer\Model\CustomerFactory');
$websiteId = $set_storeManager->getWebsite()->getWebsiteId();

/// Get Store ID
$store = $set_storeManager->getStore();
$storeId = $store->getStoreId();

// Instantiate object (this is the most important part)
$customer = $get_customerFactory->create();

// set user details
$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = " ingo@onlinecode"; // add  Email id
$setPassword = "onlinecode@123456789"; // add  password

// Preparing data for new customer

// set user location details
$setPostcode = "989898"; // add  Post code
$setCity = "Sydney "; // add  city of user
$setRegion = "New South Wales";
$setTelephone = "99999999999";
$setFax = "123456";
$setCompany = "Australia";
$setStreet = "in Australia some place";

// Save customer data
echo 'Succesfully Saved'.$customer->getId();
// Add Address For created customer
$object_addres = $set_objectManager->get('\Magento\Customer\Model\AddressFactory');
$set_address = $object_addres->create();

// if Customer country is USA then need add state / province
// save Customer address
catch (Exception $exception) {
catch(Exception $exception)
// error message

Hope this code and post will helped you for implement magento2 update customer address programmatically. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US