Magento 2 generate invoice automatically which is an important element in Magento transactions. Store owners need to send invoices to customers quickly for them to know the contents and amount of the items they purchased. Normally, the system automatically makes an invoice by itself when you pay online. However, if you pay with something like a check or money order, the store owner has to make the invoice by hand. To address this case, this article will show you how to Magento 2 create order invoice automatically with simple steps.
How to Magento 2 Generate Invoice Automatically
Table of Contents
For Magento 2 generate invoice automatically, we will show you the two simplest ways:
- Magento 2 Create Invoice Programmatically
- Generates invoices through Magento 2 extension
Method 1: Magento 2 Create Invoice Programmatically
Basically, there are 2 steps to create invoice Magento 2 programmatically
Step 1: Create a New Model
Navigate to app/code/Vendor/Module/Model/CreateInvoice.php, and use the following code:
<?php namespace [Vendor]\[Module]\Model; 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 CreateInvoice { protected $orderRepository; protected $invoiceService; protected $transaction; protected $invoiceSender; public function __construct( OrderRepositoryInterface $orderRepository, InvoiceService $invoiceService, InvoiceSender $invoiceSender, Transaction $transaction ) { $this->orderRepository = $orderRepository; $this->invoiceService = $invoiceService; $this->transaction = $transaction; $this->invoiceSender = $invoiceSender; } public function execute($orderId) { $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); $order->addCommentToStatusHistory( __('Notified customer about invoice creation #%1.', $invoice->getId()) )->setIsCustomerNotified(true)->save(); } } }
Step 2: Use ObjectManager to Quickly Test
After that, you can use ObjectManager to quickly test how it works:
\Magento\Framework\App\ObjectManager::getInstance() ->create(\Vendor\Module\Model\CreateInvoice::class) ->execute(4);
Above is the detailed process of how to create invoice programmatically in Magento 2. Now, we will move to the second method below
Method 2: Magento 2 Generates Invoices Through the Extension
We know that creating Magento 2 generates invoices by coding is still difficult for newbies without tech knowledge. Thus, we highly recommend Magento 2 Auto Invoice & Shipment from BSS Commerce.
With this extension, you can easily generate invoice and shipment automatically, leading to speeds up Magento 2 payment process.
Some highlight features of Magento 2 Auto Invoice & Shipment:
- Magento 2 send invoice email automatically for invoices and shipments
- Magento 2 generate invoice/ shipment automatically
- Compatible with multiple payment methods
- Accepted PDF File To Invoices/Shipment Email
- Select capture for online payment method
- Create order shipment independent of order invoice
Additionally, don’t forget to check out other powerful Magento 2 extensions from BSS Commerce!
Conclusion
To sum up, the step-by-step guide to Magento 2 generates invoices automatically. BSS Commerce introduces to you the above that makes your order processing easy and quick. Whether having tech knowledge or not, with these guides, you will find the most suitable method for your store.
If you are looking for a highly reliable Magento agency to enhance your store performance, you can contact BSS Commerce for support. Throughout more than 11 years, we have helped numerous clients evolutionize their Magento store with exceptional products and services. For this reason, BSS Commerce has proudly received 5-star rating on the TrustPilot platform from happy customers around the world.
At the moment, we offers a variety of Magento solutions below:
- Magento web development services
- Magento PWA development
- Magento 2 speed optimization services
- Magento 2 upgrade services