In Magento 2 we can get all the regions list by country code. We can get all the regions of the specific country by Country code.
Magento\Directory\Model\Country is used to getting all the regions list by the country code.
The output is an array with value, title, country code and label for regions.
Create Block file to define Country class,
<?php namespace Rbj\Regions\Block; class Regions extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Model\Country $country array $data = [] ) { $this->country = $country; parent::__construct($context, $data); } /** * Get the list of regions present in the given Country * Returns empty array if no regions available for Country * * @param String * @return Array/Void */ public function getRegionsOfCountry($countryCode) { $regionCollection = $this->country->loadByCode($countryCode)->getRegions(); $regions = $regionCollection->loadData()->toOptionArray(false); return $regions; }
call from a template file,
$block->getRegionsOfCountry('usa'); // pass two or three character country code..
If country have states available in database they will display result with all the states otherwise they will returns all the regions of country in database.
For US,Result will be like,
Array ( [0] => Array ( [title] => [value] => [label] => Magento\Framework\Phrase Object ( [text:Magento\Framework\Phrase:private] => Please select a region, state or province. [arguments:Magento\Framework\Phrase:private] => Array ( ) ) ) [1] => Array ( [value] => 1 [title] => Alabama [country_id] => US [label] => Alabama ) [2] => Array ( [value] => 2 [title] => Alaska [country_id] => US [label] => Alaska ) [3] => Array ( [value] => 3 [title] => American Samoa [country_id] => US [label] => American Samoa ) [4] => Array ( [value] => 4 [title] => Arizona [country_id] => US [label] => Arizona ) [5] => Array ( [value] => 5 [title] => Arkansas [country_id] => US [label] => Arkansas ) [6] => Array ( [value] => 6 [title] => Armed Forces Africa [country_id] => US [label] => Armed Forces Africa ) [7] => Array ( [value] => 7 [title] => Armed Forces Americas [country_id] => US [label] => Armed Forces Americas ) [8] => Array ( [value] => 8 [title] => Armed Forces Canada [country_id] => US [label] => Armed Forces Canada ) [9] => Array ( [value] => 9 [title] => Armed Forces Europe [country_id] => US [label] => Armed Forces Europe ) [10] => Array ( [value] => 10 [title] => Armed Forces Middle East [country_id] => US [label] => Armed Forces Middle East ) [11] => Array ( [value] => 11 [title] => Armed Forces Pacific [country_id] => US [label] => Armed Forces Pacific ) [12] => Array ( [value] => 12 [title] => California [country_id] => US [label] => California ) [13] => Array ( [value] => 13 [title] => Colorado [country_id] => US [label] => Colorado ) [14] => Array ( [value] => 14 [title] => Connecticut [country_id] => US [label] => Connecticut ) [15] => Array ( [value] => 15 [title] => Delaware [country_id] => US [label] => Delaware ) [16] => Array ( [value] => 16 [title] => District of Columbia [country_id] => US [label] => District of Columbia ) [17] => Array ( [value] => 17 [title] => Federated States Of Micronesia [country_id] => US [label] => Federated States Of Micronesia ) [18] => Array ( [value] => 18 [title] => Florida [country_id] => US [label] => Florida ) [19] => Array ( [value] => 19 [title] => Georgia [country_id] => US [label] => Georgia ) [20] => Array ( [value] => 20 [title] => Guam [country_id] => US [label] => Guam ) [21] => Array ( [value] => 21 [title] => Hawaii [country_id] => US [label] => Hawaii ) [22] => Array ( [value] => 22 [title] => Idaho [country_id] => US [label] => Idaho ) [23] => Array ( [value] => 23 [title] => Illinois [country_id] => US [label] => Illinois ) [24] => Array ( [value] => 24 [title] => Indiana [country_id] => US [label] => Indiana ) [25] => Array ( [value] => 25 [title] => Iowa [country_id] => US [label] => Iowa ) [26] => Array ( [value] => 26 [title] => Kansas [country_id] => US [label] => Kansas ) [27] => Array ( [value] => 27 [title] => Kentucky [country_id] => US [label] => Kentucky ) [28] => Array ( [value] => 28 [title] => Louisiana [country_id] => US [label] => Louisiana ) [29] => Array ( [value] => 29 [title] => Maine [country_id] => US [label] => Maine ) [30] => Array ( [value] => 30 [title] => Marshall Islands [country_id] => US [label] => Marshall Islands ) [31] => Array ( [value] => 31 [title] => Maryland [country_id] => US [label] => Maryland ) [32] => Array ( [value] => 32 [title] => Massachusetts [country_id] => US [label] => Massachusetts ) [33] => Array ( [value] => 33 [title] => Michigan [country_id] => US [label] => Michigan ) [34] => Array ( [value] => 34 [title] => Minnesota [country_id] => US [label] => Minnesota ) [35] => Array ( [value] => 35 [title] => Mississippi [country_id] => US [label] => Mississippi ) [36] => Array ( [value] => 36 [title] => Missouri [country_id] => US [label] => Missouri ) [37] => Array ( [value] => 37 [title] => Montana [country_id] => US [label] => Montana ) [38] => Array ( [value] => 38 [title] => Nebraska [country_id] => US [label] => Nebraska ) [39] => Array ( [value] => 39 [title] => Nevada [country_id] => US [label] => Nevada ) [40] => Array ( [value] => 40 [title] => New Hampshire [country_id] => US [label] => New Hampshire ) [41] => Array ( [value] => 41 [title] => New Jersey [country_id] => US [label] => New Jersey ) [42] => Array ( [value] => 42 [title] => New Mexico [country_id] => US [label] => New Mexico ) [43] => Array ( [value] => 43 [title] => New York [country_id] => US [label] => New York ) [44] => Array ( [value] => 44 [title] => North Carolina [country_id] => US [label] => North Carolina ) [45] => Array ( [value] => 45 [title] => North Dakota [country_id] => US [label] => North Dakota ) [46] => Array ( [value] => 46 [title] => Northern Mariana Islands [country_id] => US [label] => Northern Mariana Islands ) [47] => Array ( [value] => 47 [title] => Ohio [country_id] => US [label] => Ohio ) [48] => Array ( [value] => 48 [title] => Oklahoma [country_id] => US [label] => Oklahoma ) [49] => Array ( [value] => 49 [title] => Oregon [country_id] => US [label] => Oregon ) [50] => Array ( [value] => 50 [title] => Palau [country_id] => US [label] => Palau ) [51] => Array ( [value] => 51 [title] => Pennsylvania [country_id] => US [label] => Pennsylvania ) [52] => Array ( [value] => 52 [title] => Puerto Rico [country_id] => US [label] => Puerto Rico ) [53] => Array ( [value] => 53 [title] => Rhode Island [country_id] => US [label] => Rhode Island ) [54] => Array ( [value] => 54 [title] => South Carolina [country_id] => US [label] => South Carolina ) [55] => Array ( [value] => 55 [title] => South Dakota [country_id] => US [label] => South Dakota ) [56] => Array ( [value] => 56 [title] => Tennessee [country_id] => US [label] => Tennessee ) [57] => Array ( [value] => 57 [title] => Texas [country_id] => US [label] => Texas ) [58] => Array ( [value] => 58 [title] => Utah [country_id] => US [label] => Utah ) [59] => Array ( [value] => 59 [title] => Vermont [country_id] => US [label] => Vermont ) [60] => Array ( [value] => 60 [title] => Virgin Islands [country_id] => US [label] => Virgin Islands ) [61] => Array ( [value] => 61 [title] => Virginia [country_id] => US [label] => Virginia ) [62] => Array ( [value] => 62 [title] => Washington [country_id] => US [label] => Washington ) [63] => Array ( [value] => 63 [title] => West Virginia [country_id] => US [label] => West Virginia ) [64] => Array ( [value] => 64 [title] => Wisconsin [country_id] => US [label] => Wisconsin ) [65] => Array ( [value] => 65 [title] => Wyoming [country_id] => US [label] => Wyoming ) )