How to create an invoice without updating total paid of order.

Here in this blog, the following 2 steps helps you to create an invoice without updating 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. In case of using Partial Payment, customers can make payment many times for an order.

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

Create an invoice in Magento

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

/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 of order in Magento 2.

 

Last Update: February 22, 2019  

February 5, 2018   386   Nandini R    Orders    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

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

Facebook
Twitter
INSTAGRAM
LinkedIn