1

I want to display only USA states as a select drop-down in custom phtml in Magento2.2.5?

Any help would be appreciated.

0

Add following code in your block class

<?php
    ...

    private $directoryData;

    public function __construct(
        ...
        \Magento\Directory\Block\Data $directoryData
        ...
    ) {
        ...
        $this->directoryData = $directoryData;
        ...
    }

    public function getStates()
    {
        return $this->directoryData->getRegionHtmlSelect();
    }
}

Add following code in your PHTML file

<?php

echo $block->getStates();

Extra Notes:

You can use following code to get the complete data.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$states = $objectManager->create('Magento\Directory\Model\RegionFactory')
        ->create()->getCollection()->addFieldToFilter('country_id','US');
echo "<pre>";
print_r($states->getData());

after get dropdown list

<select>
<?php foreach ($states as $_state):?>
  <option value="<?php echo $_state->getCode();?>"><?php echo $_state->getName();?></option>
  <?php endforeach;?>
</select>

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.