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:
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; }
Also, do not forget to share the solution with fellow Magento developers via social media.
Thank you.