{"id":7391,"date":"2019-10-04T12:29:01","date_gmt":"2019-10-04T06:59:01","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7391"},"modified":"2019-10-04T15:50:07","modified_gmt":"2019-10-04T10:20:07","slug":"magento-2-get-list-all-countries","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-list-all-countries\/","title":{"rendered":"Magento 2 &#8211; How to Get List of All Countries"},"content":{"rendered":"<p>In this post I am going to explain how to get country collection in Magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_countryCollectionFactory;\r\n    \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,\r\n\t\\Magento\\Directory\\Model\\ResourceModel\\Country\\CollectionFactory $countryCollectionFactory,\r\n\tarray $data = []\r\n) \r\n{\r\n\t$this-&gt;_countryCollectionFactory = $countryCollectionFactory;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getCountryCollection()\r\n{\r\n\t$collection = $this-&gt;_countryCollectionFactory-&gt;create()-&gt;loadByStore();\r\n\treturn $collection;\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Get country collection\r\n$countryCollection = $block-&gt;getCountryCollection();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($countryCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";<\/pre>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$countryCollectionFactory = $objectManager-&gt;get('Magento\\Directory\\Model\\ResourceModel\\Country\\CollectionFactory');\r\n\r\n\/\/ Get country collection\r\n$countryCollection = $countryCollectionFactory-&gt;create()-&gt;loadByStore();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($countryCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get country collection in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_countryCollectionFactory; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Directory\\Model\\ResourceModel\\Country\\CollectionFactory $countryCollectionFactory, array $data = [] ) { $this-&gt;_countryCollectionFactory = $countryCollectionFactory; parent::__construct($context, $data); } public function getCountryCollection() { $collection = $this-&gt;_countryCollectionFactory-&gt;create()-&gt;loadByStore();&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-list-all-countries\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7416,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7391"}],"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=7391"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7391\/revisions"}],"predecessor-version":[{"id":7392,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7391\/revisions\/7392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7416"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}