0

How to get order details by invoice id? I need order details by invoice id.

I got this how to get order deatils by invoice id? but this is for magento 1

  • You should use factory method instead of object manager. Check my answer – Prince Patel May 29 '18 at 6:25
1

In magento2 this can be achieves as :

protected $objectManager;

public function __construct(
     .....
    \Magento\Framework\ObjectManagerInterface $objectManager,
     .......
    ) {
     ...
     $this->objectManager = $objectManager;
     ........
   }


  public function getOrder($invoice_id){
    $invoice= $this->objectManager->create('Magento\Sales\Model\Order\Invoice')->load($invoice_id);
    return $invoice->getOrder();
  } 
  • you should not use the objectmanager in production though ;) better require the real class in your constructor – simonthesorcerer May 28 '18 at 21:29
1

Use factory method instead of object manager

protected $invoiceFactory;

public function __construct(
    ...
    \Magento\Sales\Model\Order\InvoiceFactory $invoiceFactory
    ...
) {
    ...
    $this->invoiceFactory = $invoiceFactory;
    ...
}

public function getOrderByInvoice($invoiceId)
{
    $invoice = $this->invoiceFactory->create()->load($invoiceId);
    return $invoice->getOrder();
}

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.