magento 2 get all customers
Today we talk about how Magento 2 get all customers. Sometimes you need to list down all the customers for many reasons. For exporting the customer the data or show all customer listing. So let start with our example to list down all the customer.
namespace QaisarSatti/HelloWorld/Block;
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerCollection;
public function __construct(
\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollection
)
{
$this->customerCollection = $customerCollection;
}
public function getAllCustomers()
{
return $this->customerCollection->create();
}
}
use Magento\Backend\App\Action;
class Delete 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 and list down all the customer data.
$allCustomers = $this->getAllCustomers();
foreach ($allCustomers as $customer) {
echo $customer->getEmail();
echo $customer->getFirstname();
echo $customer->getLastname();
}
foreach ($allCustomers as $customer) {
echo $customer->getEmail();
echo $customer->getFirstname();
echo $customer->getLastname();
}