Here we are mentioning to set the state code from the country code and state id from the Netsuite API.
We are getting the state and country through the response/params from the API
$resultJson = $this->_resultJsonFactory->create();
$params = $this->getRequest()->getContent();
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/Address_update.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$response = array(
'status' => 500,
'message' => 'Error, Please try again',
'params' => $params
);
if(!empty($params)) {
$addresss=$params;
$addresss= json_decode($addresss,true);
$logger->info(print_r($addresss,true));
//setting the state from country code and state code
if(!empty($addresss)){
$region=$addresss['State'];
if($region){
$regionCode = $this->collectionFactory->create()
->addRegionCodeFilter($region)
->addCountryFilter($addresss['Country'])
->getFirstItem()
->getData('region_id');
$logger->info($regionCode);
}
$address = $this->addressfactory->create()->load($addresss['id'],'entity_id');
$logger->info($addresss['id']);
$logger->info($address->getEntityId());
if($address->getEntityId() == $addresss['id']){
$address->setCustomerId($addresss['customer_id'])
->setFirstname($addresss['firstname'])
->setLastname($addresss['lastname'])
->setCountryId($addresss['Country'])
->setPostcode($addresss['Post_Code'])
->setCity($addresss['City'])
->setTelephone($addresss['telephone'])
->setStreet($addresss['street']);
if($regionCode){
$address->setRegionId($regionCode);
}
if($addresss['set_is_default_shipping'] == 1){
$logger->info("default_shippping");
$address->setIsDefaultShipping(1);
}
if($addresss['set_is_default_billing'] == 1){
$logger->info("default_billing");
$address->setIsDefaultBilling(1);
}
$address->save();
$response = array(
'status' => 200,
'message' => 'Customer Address Updated',
'params' => $params
);
}