How to Get Invoice Data From Order ID in Magento 2
Invoices in Magento 2 is a record of the payment for an order.
An admin can create multiple invoices for a single order. However, when you want to implement a functionality per order only, based on invoice data, you’ll need to get invoice data from order id in Magento 2. Likewise you can also create and add barcode n Magento 2 invoice PDF, it helps the customer to scan and get the order id using which it is easy to get order details.
For example, you want to implement payment fees to be charged on each order. For a particular order, the payment fee is to be charged on the first invoice only.
To implement such a feature, you can use the below solution to get invoice data from order ID in Magento 2.
Method to get invoice data from order ID in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use MagentoSalesModelOrderFactory; protected $orderFactory; public function __construct(OrderFactory $orderFactory) { $this->orderFactory = $orderFactory; } public function getAllInvoice($orderId) { $order = $this->orderFactory->create()->load($orderId); $invoiceCollection = $order->getInvoiceCollection(); return $invoiceCollection; } |
Any doubts? If so, please use the Comments section below to mention them and I’ll gladly solve them for you.
Also, do not forget to share the solution with fellow Magento developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Get Data From Custom Database Table in Magento 2
How to Enable/Disable Remember Me Functionality in Magento
Next