How to get Customer Collection in Magento 2

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.

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x