0

I'm trying to get fetch Region Name instead of its value, from select option, but I get the only region value not its name,

I have created a select option in the system.xml

below is my code,

System.xml

<field id="gstregion" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Production State</label>
                <comment><![CDATA[Select the Origin State.]]></comment>                 
                <source_model>Test\Code\Model\Config\Region\RegionInformationProvider</source_model>
                <depends>
                    <field id="GSTtax/GSTtax/status">1</field>
                </depends>
            </field>

app/code/Test/Code/Model/Config/Region

<?php
namespace Test\Code\Model\Config\Region;

class RegionInformationProvider
{
  protected $countryInformationAcquirer;
  protected $addressRepository;

  public function __construct(
      \Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformationAcquirer
  ) {
         $this->countryInformationAcquirer = $countryInformationAcquirer;
  }

  public function toOptionArray()
  {
        $countries = $this->countryInformationAcquirer->getCountriesInfo();
        foreach ($countries as $country) {
            if($country->getId() == 'IN'){
                $regions = [];
                if ($availableRegions = $country->getAvailableRegions()) {
                     foreach ($availableRegions as $region) {
                          $regions[] = [
                             'value' => $region->getId(),
                             'label' => $region->getName()
                           ];
                     }
                 }
            }  
        }
        return $regions;

     }
}

Data.php

const CONFIG_GST_REGION           = 'GSTtax/GSTtax/gstregion';

public function getGstRegion()
    {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        return $this->scopeConfig->getValue(self::CONFIG_GST_REGION, $storeScope);
    }

test.phtml

$state = $this->helper('Test\Code\Helper\Data')->getGstRegion();    
echo $state;

<option value="580" id="optIdqnoKrZDb" selected="selected">Gujarat</option>

Output => 580

not Gujarat

4 Answers 4

1

change this line from

'value' => $region->getId(),

to

'value' => $region->getName(),

then check

5
  • not worked yet, i have changed as per your answer.
    – rocky9310
    Dec 14, 2021 at 6:17
  • did you save the region again from configuration again and flush the cache?
    – Mehran
    Dec 14, 2021 at 6:19
  • yes, i have doing it same, but its not worked, its only give me a value
    – rocky9310
    Dec 14, 2021 at 6:24
  • now its work, thanks you saved my time
    – rocky9310
    Dec 14, 2021 at 6:32
  • You're welcome.
    – Mehran
    Dec 14, 2021 at 6:46
0

Yes, you will get this id as value getting store is value not name.

So you have to get the name of that region. Pass your $state into this will load out all data related to the id

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$region = $objectManager->create('Magento\Directory\Model\Region')
                        ->load($state);

$region->getName();

Note Please avoid use of object manager

This might help you. More Information: Link

Thanks

0

this is the correct way to show the region option

<?php
namespace Test\Code\Model\Config\Region;

class RegionInformationProvider
{
  protected $_regionsFactory;
  protected $country;

  public function __construct(
      \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionsFactory,
      \Magento\Directory\Model\Country $country

  ) {
        $this->_regionsFactory = $regionsFactory;
        $this->country = $country;
  }

     public function toOptionArray() {
        $regions = [];
        $regionCollection = $this->country->loadByCode('IN')->getRegions();
        $regions = $regionCollection->loadData()->toOptionArray(false);
        foreach ($regions as $region){
            $regions[] = [
                            'value' => $region['label'],
                            'label' => $region['label']
                        ];
         } 

        return $regions;
    }
}   
0

To get the username you can follow the below code.

use Magento\Directory\Model\ResourceModel\Region\Collection  as RegionCollection;

class Example
{
    /** @var RegionCollection  */
    private $regionCollection;
    
    public function __construct(
        RegionCollection $regionCollection
    ) {
        $this->regionCollection = $regionCollection;
    }

   
    private function getRegionName()
    {
        $region = $this->regionCollection->getData();
        foreach ($regions as $region) {
            $regionName = $region['name'];
        }
    }
}

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.