{"id":874,"date":"2020-03-07T02:30:26","date_gmt":"2020-03-07T02:30:26","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/07\/create-invoice-programmatically-in-magento-2\/"},"modified":"2025-05-22T14:49:54","modified_gmt":"2025-05-22T09:19:54","slug":"create-invoice-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-invoice-programmatically-in-magento-2\/","title":{"rendered":"How To Programmatically Create Invoice In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Are you offering products where the order status does not depend on the total amount paid? Are you offering the facility of&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-partial-payment.html\" target=\"_blank\" rel=\"noreferrer noopener\">partial payments in Magento 2<\/a>&nbsp;store?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every time a partial payment installment is paid, Magento 2 considers an order fulfilled and generates an invoice. Mess, right?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<em><strong>programmatically create invoice in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement the below code for the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to programmatically create invoice in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>routes.xml<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/etc\/frontend<\/strong>&nbsp;Folder add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;config xmlns_xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi_noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n\u00a0\u00a0 \u00a0&lt;router id=\"standard\">\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&lt;route frontName=\"helloworld\" id=\"helloworld\">\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&lt;module name=\"[Vendor]_[Module]\"\/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&lt;\/route>\n\u00a0\u00a0 \u00a0&lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>Invoice.php<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/Controller<\/strong>&nbsp;Folder add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace [Vendor][Module]Controller;\n \nuse MagentoFrameworkAppActionContext;\nuse MagentoFrameworkAppActionAction;\nuse MagentoSalesApiOrderRepositoryInterface;\nuse MagentoSalesModelServiceInvoiceService;\nuse MagentoFrameworkDBTransaction;\nuse MagentoSalesModelOrderEmailSenderInvoiceSender;\nclass Invoice extends Action\n{\n    protected $orderRepository;\n    protected $invoiceService;\n    protected $transaction;\n    protected $invoiceSender;\n\n    public function __construct(\n        Context $context,\n        OrderRepositoryInterface $orderRepository,\n        InvoiceService $invoiceService,\n        InvoiceSender $invoiceSender,\n        Transaction $transaction\n    )\n    {\n        $this->orderRepository = $orderRepository;\n        $this->invoiceService = $invoiceService;\n        $this->transaction = $transaction;\n        $this->invoiceSender = $invoiceSender;\n        parent::__construct($context);\n    }\n\n    public function execute()\n    {\n        $orderId = 174; \/\/it should be order id\n        $order = $this->orderRepository->get($orderId);\n        if ($order->canInvoice()) {\n            $invoice = $this->invoiceService->prepareInvoice($order);\n            $invoice->register();\n            $invoice->save();\n            $transactionSave = $this->transaction->addObject(\n                $invoice\n            )->addObject(\n                $invoice->getOrder()\n            );\n            $transactionSave->save();\n            $this->invoiceSender->send($invoice);\n\t\t\t\/\/Send Invoice mail to customer\n            $order->addStatusHistoryComment(\n                __('Notified customer about invoice creation #%1.', $invoice->getId())\n            )\n                ->setIsCustomerNotified(true)\n                ->save();\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, call controller as per your requirement<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow Magento developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you offering products where the order status does not depend on the total amount paid? Are you offering the facility of&nbsp;partial payments in Magento&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-874","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=874"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/874\/revisions"}],"predecessor-version":[{"id":15046,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/874\/revisions\/15046"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}