How to create an invoice for the total paid order in Magento 2?

Here, the following 2 steps help you to create an invoice without updating the total paid of order in Magento 2. They are:

  • Use event sales_order_invoice_pay
  • Write the observer class

In Magento, the complete paid by Orders will be maximized after shaped an invoice. In that way all time you built a new invoice, Magento records that you earned money from customers. Just in case if you’re using Partial Payment, customers can make a payment many times for an order.

Build your Amazon-like multi-vendor marketplace website @ $249/- now!!!
             Get your free live demo now: Click here

So how can we create the invoice without updating the total paid of order, just follow the below steps :

Create an invoice in Magento 2 platform

Step 1: Use event sales_order_invoice_pay

State observer in event.xml for your custom

module: /app/code/[NameSpace]/[ModuleName]/etc/events.xml

<?xml version="1.0"?>

    <event name="sales_order_invoice_pay">

        <observer name="webpos_sales_order_invoice_pay" instance="[NameSapce]\[ModuleName]\Observer\SalesOrderInvoicePay" />

    </event>  

</config>

Step 2: write the observer class in Magento 2

/app/code/[NameSpace]/[ModuleName]/Observer/SalesOrderInvoicePay.php
<?php

namespace [NameSpace]\[ModuleName]\Observer\Sales;

use Magento\Framework\Event\Observer as EventObserver;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderInvoicePay implements ObserverInterface

{   

/**

* @param EventObserver $observer

* @return $this

* @SuppressWarnings(PHPMD.CyclomaticComplexity)

*/

public function execute(EventObserver $observer)

{

     $invoice = $observer->getEvent()->getInvoice();

     $order = $invoice->getOrder();

     /* reset total_paid & base_total_paid of order */

     $order->setTotalPaid($order->getTotalPaid() - $invoice->getGrandTotal());

     $order->setBaseTotalPaid($order->getBaseTotalPaid() - $invoice->getBaseGrandTotal());

}    

}

I hope the above information will help you to create an invoice without updating the total paid order in Magento 2.

Click to get started with your eCommerce store with Magento 2 platform.

Create an invoice in Magento 2


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *