Get Invoice details by order increment id programmatically Magento 2.

You can get the invoice data for the specific order by order increment id in Magento 2.

Using Magento\Sales\Api\InvoiceRepositoryInterface interface, you need to use getList() function to fetch no. of invoice by sales order increment id.

When you check the sales_invoice table, order_id available in sales_invoice table so you first fetch the order entity id from the order increment id.

To get invoice records, you first fetch the order id by order increment id and pass order id as SearchCriteriaBuilder addfilter() method.

Pass search criteria object to getList() method of InvoiceRepositoryInterface to fetch invoice details by order increment id.

<?php
namespace Path\To\ModelClass;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\InvoiceRepositoryInterface;

class InvoiceDataByIncrementId
{
    /**
     * @var InvoiceRepositoryInterface
     */
    private $invoiceRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
    * @var OrderRepositoryInterface
    */
    private $orderRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        InvoiceRepositoryInterface $invoiceRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        OrderRepositoryInterface $orderRepository,
        LoggerInterface $logger
    ) {
        $this->invoiceRepository = $invoiceRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->orderRepository = $orderRepository;
        $this->logger = $logger;
    }

    /**
     * Get Invoice data by Order Id
     *
     * @param $incrementId
     * @return InvoiceInterface[]|null
     */
    public function getInvoiceDataByOrderId($incrementId)
    {
        // first fetch order entity id by increment id
        $orderId = $this->getOrderIdByIncrementId($incrementId);
        if ($orderId) {
            $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('order_id', $orderId)->create();
            try {
                $invoices = $this->invoiceRepository->getList($searchCriteria);
                $invoiceRecords = $invoices->getItems();
            } catch (Exception $exception)  {
                $this->logger->critical($exception->getMessage());
                $invoiceRecords = null;
            }
        }
        return $invoiceRecords;
    }

    /**
     * Get Order entity id by Order Increment Id
     * @param $incrementId
     * @return int|null
     */
    public function getOrderIdByIncrementId($incrementId)
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('increment_id', $incrementId)->create();
        $order = $this->orderRepository->getList($searchCriteria);
        $orderId = null;
        if ($order->getTotalCount()) {
            foreach ($order->getItems() as $orderData) {
                $orderId = (int)$orderData->getId();
            }
        }
        return $orderId;
    }
}

You can get a result as Invoice data by the above function,

$orderIncrementId = 000000001; // order increment_id
$invoices = $this->getInvoiceDataByOrderId($orderIncrementId);

foreach ($invoices as $invoice) {
    # code...
    var_dump($invoice);
}

You got the invoice object for the var_dump in the above output.