cancel
Showing results for 
Search instead for 
Did you mean: 

Get order history by customer email

Get order history by customer email

I am looking for a code sample on how to get order history by customer email

 

This is the code I am working with ATM:

	public function __construct(
			\Magento\Customer\Model\CustomerRegistry $customerRegistry,
			\Magento\Backend\Block\Template\Context $context,
			\Magento\Store\Model\StoreManagerInterface $storeManager,
			\Magento\Sales\Model\OrderFactory $orderFactory,
			\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
	) {
		$this->customerRegistry = $customerRegistry;
		$this->_scopeConfig = $scopeConfig;
		$this->adminUser 	= $this->_scopeConfig->getValue('customer_order_api_control/apioptions/api_user', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
		$this->adminPassword= $this->_scopeConfig->getValue('customer_order_api_control/apioptions/api_password', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
		
		$this->storeManager	= $storeManager;
		$this->orderFactory = $orderFactory;
		$this->baseUrl 		= $this->storeManager->getStore()->getBaseUrl();
		$this->restUrl 		= $this->baseUrl . '/index.php/rest/V1/';
		
	}
	
    public function getOrders($customerId) {
    	/*$customerModel = $this->customerRegistry->retrieve($customerId);
        return $customerModel->getDataModel();*/
    	
    	/*$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
		$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
		$connection = $resource->getConnection();
		$tableName = $resource->getTableName('customer_entity');*/
		
		$orderFactory->getSalesOrderCollection(array('email'=>$customerId));
		$sql = "SELECT email FROM " . $tableName . " WHERE entity_id = '".$customerId."'";
		$email = $connection->fetchOne($sql);
		
		$orders = array();
		if (!empty($email)) {
			$orders = $this->getOrdersByEmail($email);
		}
		
		$orders = json_encode($orders);
		return $orders;
    }
2 REPLIES 2

Re: Get order history by customer email

Hey RonBongo, 

 

You can try this logic:

<?php

protected $_orderFactory;

public function __construct(
 .....
 \Magento\Sales\Model\OrderFactory $orderFactory
 .....

) {
 ...
 $this->_orderFactory = $orderFactory;
 ....
}

// Customer email as a parementer of method
public function getOrders($customerEmail)
{
 //Retrieve all orders with this email address
 $orders = $this->_orderFactory->create()
        ->getCollection()
  ->addFieldToFilter('customer_email', $customerEmail);

 //Retrieve all order ids
 $orderIds = $order->getAllIds();
}

Kind of the same logic is used in our Advanced Reviews and Reminders module  for Magento 1. 

 

Regards,
Plumrocket Team

Re: Get order history by customer email

Thanks! 

 

This was helpful. I ended up doing something similar.

 

I am going to encode the output as this is an API.

 

Do you know if there is an easy way to re-use the method magento 2 uses with the admin API?

 

This is to work the same way but within the customer context.