5

I am trying to get order collection by order id in Magento 2.

Here is my code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->load($oid);

echo $custLastName= $orders->getCustomerLastname();

But its is giving fatal error:

 Call to undefined method Magento\Sales\Model\ResourceModel\Order\Collection::getCustomerLastname()
  • 1
    use $orders = $objectManager->create('Magento\Sales\Model\Order')->load($oid); – bang.nguyen47 Apr 3 '19 at 6:58
  • This method does not return customer information. So I need collection of it that provides customer information as well – Arshad Hussain Apr 3 '19 at 7:00
  • check my answer just remove getCollection – Rk Rathod Apr 3 '19 at 7:06
  • Do not use ObjectManager. You can follow this get Order Details using Order id In Magento 2 – Rohit Kundale Jun 26 at 5:59
7

Just Remove getCollection :-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orders = $objectManager->create('Magento\Sales\Model\Order')->load($oid);

echo $custLastName= $orders->getCustomerLastname();
| improve this answer | |
  • hi @Rathod, can we get the last itemSku from the order? if the order is placed with one or more sku. – jafar pinjar Sep 25 '19 at 7:41
  • only last item sku ? – Rk Rathod Sep 25 '19 at 7:58
  • yes @Rathod, can we get it? – jafar pinjar Sep 25 '19 at 8:20
  • im not sure but try this $order->getAllItems()->getLastItem() – Rk Rathod Sep 25 '19 at 8:33
  • No, this will give the last order item, but not the last sku in the particular order – jafar pinjar Sep 25 '19 at 8:40
4

The Following code will easily get Customer details, Billing, Shipping and order totals

  $orderId = 1222;
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
  $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

  /*get customer details*/

  $custLastName= $orders->getCustomerLastname();
  $custFirsrName= $orders->getCustomerFirstname();
  $ipaddress=$order->getRemoteIp();
  $customer_email=$order->getCustomerEmail();
  $customerid=$order->getCustomerId();

  /* get Billing details */  
  $billingaddress=$order->getBillingAddress();
  $billingcity=$billingaddress->getCity();      
  $billingstreet=$billingaddress->getStreet();
  $billingpostcode=$billingaddress->getPostcode();
  $billingtelephone=$billingaddress->getTelephone();
  $billingstate_code=$billingaddress->getRegionCode();

  /* get shipping details */

  $shippingaddress=$order->getShippingAddress();        
  $shippingcity=$shippingaddress->getCity();
  $shippingstreet=$shippingaddress->getStreet();
  $shippingpostcode=$shippingaddress->getPostcode();      
  $shippingtelephone=$shippingaddress->getTelephone();
  $shippingstate_code=$shippingaddress->getRegionCode();

 /* get  total */

  $tax_amount=$order->getTaxAmount();
  $total=$order->getGrandTotal();
| improve this answer | |
  • I placed this code in a file in a directory called "testcode" at the same level as public_html. Please tell me what should be the path \Magento\Framework\App\ObjectManager::getInstance(); – CodeForGood Apr 6 at 21:56
1

Try this,

<?php                                                                 
namespace Vendor\Module\Controller\Orders;                                
class ReadOrders extends \Magento\Framework\App\Action\Action                 
{
public function __construct(
 \Magento\Framework\App\Action\Context $context,
 \Magento\Sales\Api\OrderRepositoryInterface $orderRepo
) {
   $this->orderRepo = $orderRepo;
   parent::__construct($context);
}

public function execute()
{
   $entity_id = '12'; // its called increment id
   $order = $this->orderRepo->get($entity_id);
   echo $order->getCustomerId();
   echo $order->getCustomerFirstname();
   echo $order->getCustomerEmail();

}
}

Don't use object manager anywhere and you can use the above code in block or helper anywhere to get the collection.

Hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.