2

I am not able to get region text from address id,

 <?php $_pAddsses = $block->getDefaultBilling() ?>
 <?php echo $block->getAddressById($_pAddsses)->getRegionId() ?>      
  // output 5



 <?php echo $block->getAddressById($_pAddsses)->getRegion() ?>
   //output ""(empty)

And for my question, I don't have any region code, as many countries don't have region dropdown, like - India country dropdown doesn't have region dropdown, so in the database, the region id is 0 and region is whatever we filled in the text box for the region.

enter image description here

I checked the database it does contain region text.

I need region text to populate the region text box for which region dropdown is not available for countries.

I am not able to figure out what am I doing wrong.

2

You can try following code to get the region from the address id.

$address = $this->addressRepository->getById($addressId);
$address->getRegion()->getRegion();

where $this->addressRepository is the object of Magento\Customer\Api\AddressRepositoryInterface
$address->getRegion() will give return you the object of Magento\Customer\Api\Data\RegionInterface
and then calling getRegion() on it will give you string.

  • I am not able to print the return value $address->getRegion(); Recoverable Error: Object of class Magento\Customer\Model\Data\Region could not be converted to string – summu Mar 4 at 10:06
  • i did this <?php $regionName = $orderHelper->getRegionName($_pAddsses) ?> <?php echo "region " .$regionName ?> Recoverable Error: Object of class Magento\Customer\Model\Data\Region could not be converted to string – summu Mar 4 at 10:08
  • Try $address->getRegion()->getRegion(); I have updated the same in my answer as well. – Anshu Mishra Mar 4 at 10:17
  • why two time getRegion() – summu Mar 4 at 10:17
  • Check the updated answer. – Anshu Mishra Mar 4 at 10:18
2

In Block File :-

protected $AddressRepoInterface;

public function __construct(
\Magento\Customer\Api\AddressRepositoryInterface $AddressRepoInterface

){
    $this->AddressRepoInterface = $AddressRepoInterface;
}
public function getRegionName() {
    return $this->AddressRepoInterface->getById($addressId)->getRegion();
}

In phtml File :-

$block->getRegionName();
  • I am getting this error Call to undefined method Magento\Customer\Model\Data\Address::getRegionName() – summu Mar 4 at 9:20
  • This is my block file \Magento\Customer\Block\Address\Book – summu Mar 4 at 9:21
  • check my updated answer – Rk Rathod Mar 4 at 9:26
  • from where do I get $regionId – summu Mar 4 at 9:27
  • And for my question, i dont have any region code, as many countries doesnt have region dropdown, like - India country dropdown doesnt have region dropdown, so in database region code is 0 and region is whatever we filled in text box for region – summu Mar 4 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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