Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to get Customer Collection in Magento 2

Vinh Jacker | 03-17-2025

Get Customer Collection

Customer Collection offers various benefits to store admin. But the most noticeable advantage is that it allows you to filter all your store’s customers by attributes. Therefore, in this tutorial, I will guide you How to Get Customer Collection in Magento 2.

How to get customer collection in 2 steps:

Step 1: Get customer object

Magento provides various methods thathelp you get an object such as get it from a factory, repository, by object manager, or by direct inject it. You can use any methods that you like, however you should consider carefully before using object manager although it is simple, it is not the best one.

Below is the lines code which you can use to inject customer factory and customer objects.

class MyClass 
{
    protected $_customer;
    protected $_customerFactory;

    public function __construct(...
                                \Magento\Customer\Model\CustomerFactory $customerFactory,
                                \Magento\Customer\Model\Customer $customers
    )
    {
        ...
        $this->_customerFactory = $customerFactory;
        $this->_customer = $customers;
    }

    public function getCustomerCollection() {
        return $this->_customer->getCollection()
               ->addAttributeToSelect("*")
               ->load();
    }

    public function getFilteredCustomerCollection() {
        return $this->_customerFactory->create()->getCollection()
                ->addAttributeToSelect("*")
                ->addAttributeToFilter("firstname", array("eq" => "Max"))
                -load();
    }
}

Although inject both customer objects and customer factories can be pointless, it will be a great demonstration for you to refer to when injecting other objects.

With the first method getCustomerCollection(), a loaded collection of all customers including all attributes wibe returnedrns. However, using this method is not a good idea if you have tmanyuch attributes because of the memory limit.

To get an object from a given customer factory, the second method getFilteredCustomerCollection() is applied. With this method, you only need to add create(), you can also add a filter to filter your collection. At this time, you will receive the collection of all customers with a first name, for example, Max.

Step 2: Get customer detail

In order to get the detail of customer, you will need their ID as the customer collection need to be loaded by customer ID.

To make it easier to follow, let assume that the customer ID is 10. You will get the cutomer detail by running the following command:

$customerID = 10;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')
->load($customerID);
$customerEmail = $customerObj->getEmail();

Conclusion

In conclusion, getting a customer collection in Magento 2 is not a complicated task. It can be done in every method of your own class such as block, controller, helper, models, etc. If you change dependency injection, don’t forget to run setup:di:compile otherwise you may get the error: PHP Fatal error: Uncaught TypeError: Argument 1 passed to.

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services