Get Invoice details by order id programmatically Magento 2.

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

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

Using SearchCriteriaBuilder Class, You need to filter by order id and pass search criteria object to getList() method of InvoiceRepositoryInterface. Continue reading “Get Invoice details by order id programmatically Magento 2.”

Total Invoice count by order id Magento 2.

You can count the no. of Invoice generated for the specific order by order id in Magento 2.

Using Magento\Sales\Api\InvoiceRepositoryInterface interface you need to use getList() method with SearchCriteriaBuilder class to fetch no. of an invoice for the order.

Using Below code snippet you can get the count of invoice for an order,

<?php
namespace Path\To\Class;

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

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

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

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

    public function __construct(
        InvoiceRepositoryInterface $invoiceRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        LoggerInterface $logger
    ) {
        $this->invoiceRepository = $invoiceRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->logger = $logger;
    }
    /**
     * Get count invoices for the order.
     *
     * @param int $orderId
     * @return int
     */
    public function getCountInvoicesForOrder($orderId)
    {
        $searchCriteria = $this->searchCriteriaBuilder->addFilter('order_id', $orderId)->create();
        try {
            $invoices = $this->invoiceRepository->getList($searchCriteria);
            $totalInvoice = $invoices->getTotalCount();
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
            $totalInvoice = 0;
        }

        return $totalInvoice;
    }
}

$orderId = 1; // order id
$this->getCountInvoicesForOrder($orderId);

Using the above way, You can fetch no. of an invoice for the single order.

Delete Invoice by invoice id Magento 2.

You can delete invoice data by invoice id in Magento 2.

Using Magento\Sales\Api\InvoiceRepositoryInterface interface you need to use get() and delete() function to delete invoice by id.

You must fetch the invoice object by id and then pass the invoice object as a parameter in delete() method to delete the specific invoice.

<?php
namespace Path\To\Class;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;

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

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

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

    /**
     * Delete Invoice by invoice id
     *
     * @return bool
     */
    public function deleteInvoice(): bool
    {
        $invoiceId = 1;
        $deleteInvoice = false;
        try {
            $invoiceData = $this->invoiceRepository->get($invoiceId);
            //delete invoice by invoice object
            $deleteInvoice = $this->invoiceRepository->delete($invoiceData);
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
        }

        return $deleteInvoice;
    }
}

Using the above way you can delete the invoice using Invoice Repository.