2 Simplest Way: Magento 2 Generate Invoice Automatically

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.

open shopify store

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:

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

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

Conclusion

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. 

Write A Comment

Name