Hello Magento Friends,
Hope all are doing well! Today’s subject matter is How to Programmatically Create Invoice using Root Script in Magento 2.
When an order is placed, Magento 2 order management system generates invoices. If you are accepting online payments via PayPal, Stripe, or other payment providers, an invoice is automatically generated.
But when an order is placed with cash on delivery or offline payments, the admin needs to create an invoice manually. Magento 2 Auto Invoice and Shipment extension can come as a savior for you to eliminate creating invoices manually.
For now, follow the below steps to Programmatically Create Invoice using Root Script in Magento 2.
Steps to Programmatically Create Invoice using Root Script in Magento 2:
Step 1: Create invoicegenerate.php file in Magento root path and then add the below 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 |
<?php use Magento\Framework\AppInterface; try { require_once __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try { $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $appState->setAreaCode('frontend'); $order_repository = $objectManager->get('Magento\Sales\Api\OrderRepositoryInterface'); $invoice_service = $objectManager->get('Magento\Sales\Model\Service\InvoiceService'); $transaction_factory = $objectManager->get('Magento\Framework\DB\TransactionFactory'); $order_id = 1; // add your order id $order = $order_repository->get($order_id); if ($order->canInvoice()) { $invoice = $invoice_service->prepareInvoice($order); $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE); // two ways for CAPTURE_OFFLINE or CAPTURE_ONLINE $invoice->register(); $invoice->getOrder()->setCustomerNoteNotify(false); $invoice->getOrder()->setIsInProcess(true); $order->addCommentToStatusHistory(__('Invoice Comment Part'), false); $transactionSave = $transaction_factory->create(); $transactionSave->addObject($invoice)->addObject($invoice->getOrder()); $transactionSave->save(); print_r(' Your Invoice Generated Successfully for This Order ID :- '.$order_id); } else { print_r(' Your Invoice is not Generated for This order ID :- '.$order_id); } } catch(\Exception $e) { print_r($e->getMessage()); } |
Step 2: After adding the above code, run the below URL to check whether the invoice is created or not.
https://yourdomain/invoicegenerate.php/
Conclusion:
This way you can Programmatically Create Invoice using Root Script in Magento 2. In addition to this, you can also Add Order ID, Customer IP Address in Invoice in Magento 2.
In case of any doubts, share the details in the comment box. Also, share the article via your social media platforms and among your friend’s groups. Stay in the know for more solutions.
Happy Reading!