I'm importing my customers via code and part of that import involves addresses and regions. If the region is in the database, I have no issue in importing it, I simply get the region id by the region interface:

/** @var \Magento\Directory\Model\RegionFactory $region */
$region = $this->regionFactory->create()->loadByName($regionName, $countryId);
$regionId = $region->getRegionId();

This works. However, most of my customers have no regionId in the database.

On backend/frontend, when there is no region entry in the database, customers can fill in a text input and the region name is saved in the address. I'd like to accomplish the same thing with my code, that is: I'd like to save the region name even if there is no database entry.

How can I achieve that?

  • Take a look at magento.stackexchange.com/questions/264250/… Commented Jul 25, 2022 at 15:18
  • Thank you, but I don't need to retrieve the region by the database, that I'm already doing. I need to save the region name in the address when it's not associated to a database entry. In the frontend/backend I do this when I fill in the region input text field instead of using the dropdown. I don't know how to that via code because if I use the region name (string) with $address->setRegion($regionStringName) it gives error
    – Lou Nik
    Commented Jul 26, 2022 at 8:59

1 Answer 1


Please let me know and do give a like if you need more info.

  • To save region name in the database using customer id , please follow below.

  • Prepare a module to stimulate this functionality of code to execute on whatever basis you like , i assume you already have a module ,let me show you via a custom script which will be placed in the magento root directory.

  • Please look at this table , where the customer address gets saved , in this table there are two columns which we should consider


column = region_id when there is region_id , enter this column

column = region , when there is only region name , enter this column


use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');


function setRegionName($objectManager)
    $customerIds = ["1"];
    foreach ($customerIds as $customerId) {
        try {
            /** @var Magento\Customer\Api\AddressRepositoryInterface $customerAddressRepo */
            $customerAddressRepo = $objectManager->get('Magento\Customer\Api\AddressRepositoryInterface');
            $customerAddress = $customerAddressRepo->getById($customerId);
            /** @var Magento\Customer\Api\Data\RegionInterface $regionInterface */
            $regionInterface = $objectManager->get('Magento\Customer\Api\Data\RegionInterface');
            $region = "Chennai";
        } catch (Exception $e) {
            echo $e->getMessage();
  • It works, thank you a lot! It is weird, though, that the factory of the region is in a module (magento\directory) whille the interface is in another (magento\customer). I expect that the customerRegionInteface would be associated with a customerRegionRepository just like the customer AddressRepositoryInterface is associated with the customer AddressInterfaceFactory, but I looked at the customer module and didnt found anything regarding region factory.
    – Lou Nik
    Commented Jul 26, 2022 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.