{"id":7274,"date":"2019-09-23T10:40:41","date_gmt":"2019-09-23T05:10:41","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7274"},"modified":"2019-09-27T13:39:54","modified_gmt":"2019-09-27T08:09:54","slug":"magento-2-get-list-all-customers","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-list-all-customers\/","title":{"rendered":"Magento 2 &#8211; How to Get List of All Customers"},"content":{"rendered":"<p>In this post I am going to explain how to get list of all customers or you can say customer 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 $_customerFactory;\r\n    \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,\r\n\t\\Magento\\Customer\\Model\\CustomerFactory $customerFactory,\r\n\tarray $data = []\r\n) \r\n{\r\n\t$this-&gt;_customerFactory = $customerFactory;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getCustomerCollection() \r\n{\r\n\t$collection = $this-&gt;_customerFactory-&gt;create()-&gt;getCollection()\r\n\t\t\t-&gt;addAttributeToSelect(\"*\")\r\n\t\t\t-&gt;load();\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 customer collection\r\n$customerCollection = $block-&gt;getCustomerCollection();\r\n\r\nif ($customerCollection &amp;&amp; count($customerCollection) &gt; 0) {\r\n    foreach ($customerCollection AS $customer) {\r\n        echo $customer-&gt;getFirstname().\"&lt;br\/&gt;\";\r\n        echo $customer-&gt;getLastname().\"&lt;br\/&gt;\";\r\n        echo $customer-&gt;getEmail().\"&lt;br\/&gt;\";\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\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$customerFactory = $objectManager-&gt;create('Magento\\Customer\\Model\\CustomerFactory')-&gt;create();\r\n\r\n\/\/Get customer collection\r\n$customerCollection = $customerFactory-&gt;getCollection()\r\n        -&gt;addAttributeToSelect(\"*\")\r\n        -&gt;load();\r\n\r\nif ($customerCollection &amp;&amp; count($customerCollection) &gt; 0) {\r\n    foreach ($customerCollection AS $customer) {\r\n        echo $customer-&gt;getFirstname() . \"&lt;br\/&gt;\";\r\n        echo $customer-&gt;getLastname() . \"&lt;br\/&gt;\";\r\n        echo $customer-&gt;getEmail() . \"&lt;br\/&gt;\";\r\n    }\r\n}<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get list of all customers or you can say customer collection in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_customerFactory; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Customer\\Model\\CustomerFactory $customerFactory, array $data = [] ) { $this-&gt;_customerFactory = $customerFactory; parent::__construct($context, $data);&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-list-all-customers\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7337,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7274"}],"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=7274"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7274\/revisions"}],"predecessor-version":[{"id":7276,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7274\/revisions\/7276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7337"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}