cancel
Showing results for 
Search instead for 
Did you mean: 

how to fetch first order based on the customer details magento 2 ?

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

how to fetch first order based on the customer details magento 2 ?

Hi guys,

          i just want to fetch first order data based on the customer details magento 2 .

is this possible ? can anyone guide me ?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to fetch first order based on the customer details magento 2 ?

You need to first fetch customer id from Customer details.

Based on customer id you need to run below code for getting first order of customer placed in site.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer_id = 2;
$order = $objectManager->create('Magento\Sales\Model\Order')
		->getCollection()
		->addAttributeToFilter('customer_id', $customer_id)->getFirstItem();
echo "<pre>";print_r($order->getData());

You can get first order data based on above data.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

6 REPLIES 6

Re: how to fetch first order based on the customer details magento 2 ?

Hello @bharath553,

 

Please find out below code to get customers order by ascending then you will get easily first item.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$order = $objectManager->create('Magento\Sales\Model\Order')
	->getCollection()
	->addAttributeToFilter('customer_id', $customer_id);

--
If my answer is useful, please Accept as Solution & give Kudos

Re: how to fetch first order based on the customer details magento 2 ?

Hello @bharath553

 

<?php
namespace 'YOUR_CUSTOM_NAME_SPACE';

class YOURCALSS extends \Magento\Framework\App\Action\Action
{

    protected $_orderCollectionFactory;

    public function __construct(
        Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct($context);

    }

    }
   public function YOURFUNCTION()
   {
$email_Id = customeremailid $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*'); // You Can filter collection as $this->orderCollectionFactory->addFieldToFilter('customer_email',array('eq'=>$emailId))->getFirstItem(); } } }

 Or 

 

Fetch collection in ascending order with customer id filter, you can get customer first order using that.

 

Manish Mittal
https://www.manishmittal.com/

Re: how to fetch first order based on the customer details magento 2 ?

Hi guys, this is not working can you guide ?

Re: how to fetch first order based on the customer details magento 2 ?


To resolve these problems, Magento provides a query parameter-based syntax for REST requests that return partial responses

Re: how to fetch first order based on the customer details magento 2 ?

can you tell me how it is possible ?

Re: how to fetch first order based on the customer details magento 2 ?

You need to first fetch customer id from Customer details.

Based on customer id you need to run below code for getting first order of customer placed in site.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer_id = 2;
$order = $objectManager->create('Magento\Sales\Model\Order')
		->getCollection()
		->addAttributeToFilter('customer_id', $customer_id)->getFirstItem();
echo "<pre>";print_r($order->getData());

You can get first order data based on above data.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial