0

I am new to Magento.I want to get invoice details by its id using object manager but unable to get it. I tried this.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load('506585');
print_r($order->getData());
$invoiceCollection = $order->getInvoiceCollection();
foreach($invoiceCollection as $invoice){
print_r(invoiceCollection);
}
6
  • please check this link, magento.stackexchange.com/questions/182079/… Jul 16, 2019 at 7:52
  • I need it in object manager
    – joitr
    Jul 16, 2019 at 7:55
  • using objectmanager is not a recommended solution, please use like in the link given. Jul 16, 2019 at 7:57
  • I know but I need object manager im creating external script
    – joitr
    Jul 16, 2019 at 8:13
  • Is 506585 order id? OR you have invoice id?
    – Amit Bera
    Jul 16, 2019 at 8:14

4 Answers 4

0

If you have inovice id then use \Magento\Sales\Model\Order\InvoiceRepository for getting invoice data of that invoice.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$invoice = $objectManager->create('\Magento\Sales\Model\Order\InvoiceRepository')->get(506585);
1
  • it is throwing Requested entity doesn't exist error
    – joitr
    Jul 16, 2019 at 10:28
0

Try this, Before you order need to submit an invoice in the backend

$invoiceid=506585;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$invoice = $objectManager->create('\Magento\Sales\Model\Order\Invoice')->load($invoiceid);

/* get all data */
print_r($invoice->getData());

/*Get tax amount and subtotal */
$taxAmount=$invoice->getTaxAmount();
$subtotal=$invoice->getSubtotal();
echo $taxAmount;
echo $subtotal;

Note: Before running your script your order need to complete the invoice, Otherwise you didn't get invoice details.

Code Tested

2
  • Try my answer.code tested Jul 16, 2019 at 9:02
  • it is returning nothing
    – joitr
    Jul 16, 2019 at 10:56
0

Simply use below code.

$invoice_id = 506585;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$invoice = $objectManager->create('\Magento\Sales\Model\Order\InvoiceRepository')->get($invoice_id);

echo $invoice->getSubtotal;

print_r($invoice->getData());

Note : invoice increment id and invoice id both different things in magento.

0

Try these codes, it is tested in 2.4 as well

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$invoiceid=278945;
$invoice = $objectManager->create('\Magento\Sales\Model\Order\Invoice')->load($invoiceid);
/*Retrieve all data*/
print_r($invoice->getData());
/*Get tax amount and subtotal */
$taxAmount=$invoice->getTaxAmount();
$subtotal=$invoice->getSubtotal();

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.