5

I need order invoice details such as subTotal, Tax, shipping amount, Discount amount and Grand total..etc.

This is my code :

$orderId = $this->getRequest()->getParam('orderNum');
$order = $this->orderFactory->create()->loadByIncrementId($orderId);   
// I need to get invoice details here ...

4 Answers 4

10

As per as,magento2 standard,you can inject Sales Collection Factory class,

protected $order;
public function __construct(
.....
\Magento\Sales\Model\OrderFactory $order,
.......
        ) {
    ...
    $this->order = $order;
    ........
    parent::__construct($context, $data);
}


 public function getInvoiceDetails($order_id){
   
   $orderdetails = $this->order->create()->loadByIncrementId($order_id);
   $orderdetails->getGrandTotal(); //you can get the grandtotal like this
    foreach ($orderdetails->getInvoiceCollection() as $invoice)
        {
            $invoice_id = $invoice->getIncrementId();
        }
  }
7

you can try the below code.

$orderId = $this->getRequest()->getParam('orderNum');
$order = $this->orderFactory->create()->loadByIncrementId($orderId); 
$invoice_details = $order->getInvoiceCollection();
foreach ($invoice_details as $_invoice) {
//you can get details here.
}
1

As per Magento standard and core functionality you can use below code:

public function getInvoiceDetails($id){
        $orderdetails = $this->order->create()->loadByIncrementId($id);
        $orderdetails->getGrandTotal(); //you can get the grandtotal like this
        foreach ($orderdetails->getInvoiceCollection() as $invoice)
        {
            $invoice_id = $invoice->getIncrementId();

        }
}
1

First get $order object and then

foreach ($order->getInvoiceCollection() as $invoice) {
                        $invoiceIncrementID = $invoice->getIncrementId();

                    }

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.