{"id":11891,"date":"2020-12-21T10:00:25","date_gmt":"2020-12-21T04:30:25","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11891"},"modified":"2020-12-21T15:34:17","modified_gmt":"2020-12-21T10:04:17","slug":"get-all-regions-country-by-country-code-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/get-all-regions-country-by-country-code-magento-2\/","title":{"rendered":"How to Get All Regions of Country by Country Code in Magento 2"},"content":{"rendered":"<p>In this post I am going to explain how to get all regions of country by country code Magento 2.<\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_country;\r\n    \r\npublic function __construct(\r\n    \\Magento\\Framework\\View\\Element\\Template\\Context $context,\r\n    \\Magento\\Directory\\Model\\Country $country,\r\n    array $data = []\r\n) {\r\n    $this-&gt;_country = $country;\r\n    parent::__construct($context, $data);\r\n}\r\n\r\n\/\/ Get All Regions of Country\r\npublic function getAllRegionsOfCountry($countryCode) \r\n{\r\n    $regionCollection = $this-&gt;_country-&gt;loadByCode($countryCode)-&gt;getRegions();\r\n    $regions = $regionCollection-&gt;loadData()-&gt;toOptionArray(false);\r\n    return $regions;\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Country Code\r\n$countryCode = 'in';\r\n\r\n\/\/ Get All Regions of Country\r\n$regions = $block-&gt;getAllRegionsOfCountry($countryCode);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($regions);\r\necho \"&lt;\/pre&gt;\";<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775809918\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get all regions of country by country code Magento 2. Add below code snippet in Block class. protected $_country; public function __construct( \\Magento\\Framework\\View\\Element\\Template\\Context $context, \\Magento\\Directory\\Model\\Country $country, array $data = [] ) { $this-&gt;_country = $country; parent::__construct($context, $data); } \/\/ Get All Regions of Country public&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/get-all-regions-country-by-country-code-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":12232,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[735,9,10,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11891"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11891"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11891\/revisions"}],"predecessor-version":[{"id":12231,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11891\/revisions\/12231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/12232"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}