In this post I am going to explain how to get invoice details by invoice id in Magento 2.
Using below code snippet you can get invoice details by passing invoice id.
1. Using Dependency Injection
Add below code snippet in Block class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | protected $_invoiceRepositoryInterface; protected $_logger; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepositoryInterface, \Psr\Log\LoggerInterface $logger, array $data = [] ) { $this->_invoiceRepositoryInterface = $invoiceRepositoryInterface; $this->_logger = $logger; parent::__construct($context, $data); } public function getInvoiceDetails($invoiceId) { $invoiceData = array(); if($invoiceId > ) { try { $invoiceData = $this->_invoiceRepositoryInterface->get($invoiceId); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $invoiceData = null; } } return $invoiceData; } |
Add below code snippet in template file.
1 2 3 4 5 6 7 8 | // Invoice Id $invoiceId = 1; $invoiceData = $block->getInvoiceDetails($invoiceId); echo "<pre>"; print_r($invoiceData->getData()); echo "</pre>"; |
2. Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Invoice Id $invoiceId = 1; $invoiceRepositoryInterface = $objectManager->get('Magento\Sales\Api\InvoiceRepositoryInterface'); $logger = $objectManager->get('Psr\Log\LoggerInterface'); try { $invoiceData = $invoiceRepositoryInterface->get($invoiceId); } catch (Exception $exception) { $logger->critical($exception->getMessage()); $invoiceData = null; } echo "<pre>"; print_r($invoiceData->getData()); echo "</pre>"; |
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!
Leave a Reply