Hi everyone, Today I will guide you how to get all customers in Magento 2.

Step 1. Create block file Magepow/AllCustomers/Block/AllCustomers.php with content bellow:

<?php
namespace Magepow/AllCustomers/Block;

use Magento\Backend\App\Action;


class AllCustomers extends Action
{

    protected $customerCollection;

    public function __construct(
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollection
    )
    {
        $this->customerCollection = $customerCollection;
    }

    public function getAllCustomers()
    {
            return $this->customerCollection->create();


    }
}

Now we call the function in the template and get data.

$allCustomers = $this->getAllCustomers();
foreach ($allCustomers as $customer) {
    echo $customer->getEmail();
    echo $customer->getFirstname();
    echo $customer->getLastname();
}

If your website is having problems, please leave a comment below or contact us at Magepow Contact, the Magepow technician team will assist in solving your problem quickly. Thanks.

Hope this article will helpful for you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending