Shopping cart

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

Step 1: Get customer object

There are various methods which are provided by Magento 2 to help 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 as although is simple, it not the best one.

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

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 object and customer factory 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 which includes all attributes will be returns. However, using this method is not a good ideal if you have too much 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 customers collection in Magento 2 is not a complicated task. It can be done in every construct method of your own class such as block, controller, helper, models, etc. Finally, if you change dependency injection, don’t forget to setup:di:compile otherwise you may get the error: PHP Fatal error: Uncaught TypeError: Argument 1 passed to.





Enjoyed the tutorial? Spread it to your friends!

Comments for How to get Customer Collection in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend: