🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How To Programmatically Create Invoice In Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

Are you offering products where the order status does not depend on the total amount paid? Are you offering the facility of partial payments in Magento 2 store?

Every time a partial payment installment is paid, Magento 2 considers an order fulfilled and generates an invoice. Mess, right?

We do not want the invoice to be generated every time there is cash flow because, unfortunately, not every time an order is placed. The solution is to programmatically create invoice in Magento 2.

Implement the below code for the same.

Method to programmatically create invoice in Magento 2:

Create routes.xml in app/code/[Vendor]/[Module]/etc/frontend Folder add the following code:

<?xml version="1.0" ?>
<config xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="helloworld" id="helloworld">
            <module name="[Vendor]_[Module]"/>
        </route>
    </router>
</config>

Create Invoice.php in app/code/[Vendor]/[Module]/Controller Folder add the following code:

<?php

namespace [Vendor][Module]Controller;
 
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppActionAction;
use MagentoSalesApiOrderRepositoryInterface;
use MagentoSalesModelServiceInvoiceService;
use MagentoFrameworkDBTransaction;
use MagentoSalesModelOrderEmailSenderInvoiceSender;
class Invoice extends Action
{
    protected $orderRepository;
    protected $invoiceService;
    protected $transaction;
    protected $invoiceSender;

    public function __construct(
        Context $context,
        OrderRepositoryInterface $orderRepository,
        InvoiceService $invoiceService,
        InvoiceSender $invoiceSender,
        Transaction $transaction
    )
    {
        $this->orderRepository = $orderRepository;
        $this->invoiceService = $invoiceService;
        $this->transaction = $transaction;
        $this->invoiceSender = $invoiceSender;
        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = 174; //it should be order id
        $order = $this->orderRepository->get($orderId);
        if ($order->canInvoice()) {
            $invoice = $this->invoiceService->prepareInvoice($order);
            $invoice->register();
            $invoice->save();
            $transactionSave = $this->transaction->addObject(
                $invoice
            )->addObject(
                $invoice->getOrder()
            );
            $transactionSave->save();
            $this->invoiceSender->send($invoice);
			//Send Invoice mail to customer
            $order->addStatusHistoryComment(
                __('Notified customer about invoice creation #%1.', $invoice->getId())
            )
                ->setIsCustomerNotified(true)
                ->save();
        }
    }
}

Now, call controller as per your requirement

That’s all.

Do share the solution with fellow Magento developers via social media.

Thank you.

Sanjay Jethva Full Image
Article bySanjay 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.