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

1
  • You should use factory method instead of object manager. Check my answer Commented May 29, 2018 at 6:25

2 Answers 2

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();
  } 
1
  • you should not use the objectmanager in production though ;) better require the real class in your constructor Commented May 28, 2018 at 21:29
2

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 and acknowledge you have read our privacy policy.

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