0

In customer module need to show all customer data whatever I want?

1

1) Use \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory class to get customer collection

    <?php

protected $_customerFactory;

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

/**
 * Get customer collection
 */
public function getCustomerCollection()
{
    return $this->_customerFactory->create();
}

Then you can call:

  <?php

$customerCollection = $this->getCustomerCollection();

foreach ($customerCollection as $customer) {
    echo $customer->getEmail();
}

2) Use \Magento\Customer\Api\CustomerRepositoryInterface class to get customer by customerId or customer email

   protected $_customerRepositoryInterface;

public function __construct(
    ....
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    ....
) {
    ....
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
}

/**
 * Get Customer by id
 */
public function getCustomer($customerId)
{   
    return $this->_customerRepositoryInterface->getById($customerId);
}

/**
 * Get Customer by email
 */
public function getCustomerByEmail($customerEmail)
{   
    return $this->_customerRepositoryInterface->get($customerEmail);
}
  • If It's Useful solution please upvote and right them. – user_61205 Aug 30 '18 at 5:14
  • hey Tushar this code get last 5 order to home page ? – sanni kalariya Apr 26 at 7:07
  • I have get last 5 orders all imformation on home page what to do? without using object manager ? – sanni kalariya Apr 26 at 7:08
  • Tushar ple expain me step by step i am a begginer so I have get last 5 orders all imformation on home page what to do? without using object manager ? – sanni kalariya Apr 26 at 10:48
  • protected $OrderFactory; public function __construct(\Magento\Sales\Model\OrderFactory $OrderFactory ) {$this->OrderFactory = $OrderFactory;} – user_61205 Apr 26 at 10:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.