We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
- Home
- Blog
- eCommerce Talk
- How to Get All Orders of Customer by Email ID in Magento 2
How to Get All Orders of Customer by Email ID in Magento 2
To do so, you may need to get all orders of customers by email id in Magento 2 and this can only be accomplished with the help of Magento experts.
In order to overcome this limitation, we have come up with a programmatic solution that will help you get all orders of customers by email id in Magento 2 without wasting much time and effort.
With our programmatic solution, you can easily fetch a number of orders along with details, such as order date, product quantity, email address, phone number, and more.
This will help the admin to identify customers with maximum order so that he may send discount codes or gift Vouchers to reward the loyal customer. This can be done only by getting all orders of Customer by email id in Magento 2 web store.
Although getting all orders of customers by their email id is very complicated to do. However, we have made it pretty easier to get them all using our below programmatic solution;
Program to Get All Orders of Customer by Email ID in Magento 2
In order to easily get all order collections of Customer by email id in Magento 2, you can use the following code in your block file through Vendor\Module\Block.
<?php
namespace MageSpark\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
class CustomerOrder extends Template
{
/**
* @var array
*/
private $data;
/**
* @var Context
*/
private $context;
/**
* @var CollectionFactory
*/
private $orderCollectionFactory;
public function __construct(
Context $context,
CollectionFactory $orderCollectionFactory,
array $data = []
) {
$this->orderCollectionFactory = $orderCollectionFactory;
$this->logger = $logger;
parent::__construct($context, $data);
}
/**
* @return array
*/
public function getCustomerOrder()
{
$customerEmailId = '[email protected]'; // Use customer Email Id here
$customerOrder = $this->orderCollectionFactory->create()
->addFieldToFilter('customer_email', $customerEmailId);
return $customerOrder->getData();
}
}
And that’s it!
If you have any questions, doubts, or face any problem, just write down your comment in the comment section below. We would be happy to help.
Don’t hesitate to share our programmatic solution with Magento Community through social media.
Cheers!!