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;
 
include('/app/bootstrap.php');
$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');
$get_state->setAreaCode('frontend');
 
// 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();
$customer->setWebsiteId($websiteId);
 
// set user details
$setFirstname = "Firstname"; // add  First name
$setLastname = "setLastname"; // add Last name
$setEmail = " ingo@onlinecode.org"; // add  Email id
$setPassword = "onlinecode@123456789"; // add  password
 
 
// Preparing data for new customer
$customer->setEmail($setEmail);
$customer->setFirstname($setFirstname);
$customer->setLastname($setLastname);
$customer->setPassword($setPassword);
 
// 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";
 
 
 
try{
	// Save customer data
	$customer->save();
	echo 'Succesfully Saved'.$customer->getId();
	// Add Address For created customer
	$object_addres = $set_objectManager->get('\Magento\Customer\Model\AddressFactory');
	$set_address = $object_addres->create();
 
	$set_address->setCustomerId($customer->getId())
	->setFirstname($setFirstname)
	->setLastname($setLastname)
	->setCountryId('AT')
	// if Customer country is USA then need add state / province 
	//->setRegionId('1') 
	->setPostcode($setPostcode)
	->setCity($setCity)
	->setTelephone($setTelephone)
	->setFax($setFax)
	->setCompany('GMI')
	->setStreet($setStreet)
	->setIsDefaultBilling('1')
	->setIsDefaultShipping('1')
	->setSaveInAddressBook('1');
	try{
		$set_address->save();
                // save Customer address
	}
	catch (Exception $exception) {
		Zend_Debug::dump($exception->getMessage());
	}
}
catch(Exception $exception)
{
	Mage::log($exception->getMessage());
	// error message
	print_r($exception->getMessage());
}




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 onlincode.org