2 Simplest Way: Magento 2 Generate Invoice Automatically

by admin

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 generate invoice automatically with simple steps.

How to Magento 2 Generate Invoice Automatically

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

Step 1: Create a new model 

Navigate to app/code/Vendor/Module/Model/CreateInvoice.php, and use the following code:


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);

            $transactionSave = 
                __('Notified customer about invoice creation #%1.', $invoice->getId())

Step 2: Use ObjectManager to quickly test

After that, you can use ObjectManager to quickly test how it works:


Method 2: Magento 2 generates invoices through the extension

extension-Magento-2-generates-invoices-automaticallyWe 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


To sum up, the step-by-step guide to Magento 2 generates invoices automatically. We introduce 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 have any questions, contact us and let us know your problems. With experienced and certified Magento developers, BSS Commerce is committed to bringing high-quality products and services to optimize your business effectively. 

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.