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.
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.
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
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.
Hi guys, this is not working can you guide ?
To resolve these problems, Magento provides a query parameter-based syntax for REST requests that return partial responses
can you tell me how it is possible ?
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.