- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi guys,
i just want to fetch first order data based on the customer details magento 2 .
is this possible ? can anyone guide me ?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Magento 2 Blogs/Tutorial
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: how to fetch first order based on the customer details magento 2 ?
Hi guys, this is not working can you guide ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: how to fetch first order based on the customer details magento 2 ?
can you tell me how it is possible ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Magento 2 Blogs/Tutorial