How to Get All Orders of Customer by Email ID in Magento 2
Customer retention is very important in online business. An online store owner may want to reward his loyal customer in order to build a better customer relationship.
For doing so, the store owner may require to get all orders of customer by email id in Magento 2.
This post offers the programmatic solution for the same.
One can fetch the entire collection of orders along with the order date, total items, and related information.
The admin may reward the loyal customer with maximum order by offering a discount or gift hamper. He can decide so by getting all the orders of a customer by their Email ID in Magento 2 store.
It is pretty complex to get a specific customer’s list of orders out of the bulk of orders in Magento 2 store. However, I’ve made this task pretty simple using the below programmatic solution:
Method to Get All Orders of Customer by Email ID in Magento 2
Use the below code in your block file at Vendor\Module\Block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Vendor\Module\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Sales\Model\ResourceModel\Order\Collection; class CustomerOrder extends Template { private $data; private $context; private $orderCollectionFactory; public function __construct( Context $context, Collection $orderCollection, array $data = [] ) { $this->orderCollection = $orderCollection; $this->logger = $logger; parent::__construct($context, $data); } public function getCustomerOrder() { $customerOrder = $this->orderCollection->create() ->addAttributeToFilter('customer_email', $customerEmailId)->load(); return $customerOrder->getData(); } } |
Done!
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be glad to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Prev
How to Get Order Status Label in Magento 2
How to Programmatically Check if Customer Account is Confirmed or not in Magento 2
Next