{"id":1924,"date":"2021-10-15T11:19:33","date_gmt":"2021-10-15T11:19:33","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-customer-collection-in-magento-2\/"},"modified":"2025-07-16T17:08:04","modified_gmt":"2025-07-16T11:38:04","slug":"get-customer-collection-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-customer-collection-in-magento-2\/","title":{"rendered":"How to Get Customer Collection in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you are a\u00a0Magento 2\u00a0developer and ever need a list of customers, here\u2019s the solution for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below solution can be used to&nbsp;<em><strong>get customer collection in Magento 2<\/strong><\/em>. You can use this code when you need to implement features based on customers or their attributes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you would want to offer a special discount code based on customer groups or offer a first-time coupon code for customers who have not placed an order on your site yet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or you want to implement a condition based on the first name, last name, DOB, gender, etc. every time you want to do such customization, you will have to get customer collection in Magento 2 first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the solution for the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Customer Collection in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Customer\\Model\\Customer\n\nclass ClassName \n{\n    protected $customerCollection;\n\n    public function __construct(Customer $customerCollection)\n    {\n        $this->customerCollection = $customerCollection;\n    }\n\n    public function getCustomerCollection() {\n        return $this->customerCollection->getCollection()\n               ->addAttributeToSelect(\"*\")\n               ->load();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One step ahead, if you want to get customer collection filtered by attribute, for example, only get customer collection whose gender is female, or whose customer account was created on a specific date, you can do so using the below solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Customer Collection with Filter Based on Attributes:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Customer\\Model\\CustomerFactory\n\nclass ClassName \n{\n    protected $customerFactory;\n\n    public function __construct(CustomerFactory $customerFactory)\n    {\n        $this->customerFactory = $customerFactory;\n    }\n\n    public function getCustomerCollection() {\n        $firstname = 'xyz';\n        return $this->customerFactory->getCollection()\n               ->addAttributeToFilter(\"firstname\", $firstname))\n                -load();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, please share the post with the Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a\u00a0Magento 2\u00a0developer and ever need a list of customers, here\u2019s the solution for you. The below solution can be used to&nbsp;get customer&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1924","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1924"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1924\/revisions"}],"predecessor-version":[{"id":18016,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1924\/revisions\/18016"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}