How to Get Orders of Customer by Email ID in Magento 2

Customer interaction is most important in an online business. It is required to entertain/to reward customers at regular time intervals for a long-term relationship. There is cut-throat competition in the e-commerce world today!

To run the store smoothly and successfully, the store owner must need to communicate and present offers to customers regularly. But it is very difficult to find out particular customer orders from the whole database.

That time, it is better to get orders of customers by email id. However, I am here with the solution for you. If you know the customer's email id, then there is a way to get all orders from the customer.

Let's know how to get orders of customer by email ID in Magento 2.

Method to get orders of customer by email ID in Magento 2

⇒ To get all orders of customers, first, you must know the customer's email id.

⇒ The next thing, you need to do is, write the below code at Vendor\Addon\Block in the block file.

<?php

namespace Vendor\Addon\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()
    {
        $customerEmailId = "milopler@milople.com"; // pass customer Email Id
        $customerOrder = $this->orderCollection->create()
            ->addAttributeToFilter('customer_email', $customerEmailId)->load();

        return $customerOrder->getData();

    }
}

⇒ You are done! :)

Thank you for choosing our solution!

Stay connected with us for more solutions.