How To Programmatically Create Invoice In Magento 2
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:
1 2 3 4 5 6 7 8 |
<?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php namespace [Vendor]\[Module]\Controller; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\Action; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Service\InvoiceService; use Magento\Framework\DB\Transaction; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; 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.
Other related posts:
- How to Create Quote & Order Programmatically in Magento 2
- How to Create Shipment Programmatically in Magento 2
- How to Add Order ID in Invoice PDF in Magento 2
Any doubts on the topic? Please share them in the Comments section below. I’d be glad to help you out.
Do 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.
2 Comments
Hi Sanjay!
Great post! question what is invoiceSender, it’s and instance of a different service? ( line 42)
Regards
Hello Gonzalo,
Thank you for drawing my attention. I’ve updated the code. Please check and do let me know if you have any further doubts.
Thank you once again 🙂