Magento 2 Create Invoice Programmatically

Magento 2 Create Invoice Programmatically

In Magento a pair of, with the exception of making product and client programmatically, you’ll be able to conjointly produce invoice programmatically with ease once following the given guides within the Magento 2 Create invoice programmatically topic.

Why do Magento 2 stores got to originated the program for making the invoice? As we recognize, every time AN invoice is made, which means AN order is placed with success and at identical time, Magento can apprise we have got earned the number of cash from the client.

However, by such traditional method, we may get mass notifications from Magento 2 system additionally as produce the equivalent range of the invoices if your client implements partial payment at intervals the order. Therefore, this subject is that the nice answer to make the invoice whereas there’s no dependency on the whole paid of order.

onlinecode brings we to easy steps for that and here we are! summary of making Magento 2 produce invoice programmatically

  • Step 1: Declare event sales_order_invoice_pay
  • Step 2: Setup the observer class

Step 1: Declare event sales_order_invoice_pay

for code of Magento 2 Create Invoice programmatically we Start the settings with events.xml file in your custom module: /app/code/onlinecode/CreateInvoice/etc/events.xml

<!-- version  -->
<?xml version="1.0"?>
	<!-- event name  -->
    <event name="sales_order_invoice_pay">
		<!-- observer  name -->
        <observer name="webpos_sales_order_invoice_pay" instance="onlinecode\CreateInvoice\Observer\CreateSalesOrderInvoicePay" />
    </event>  
</config>

Step 2: Setup the observer class

for code of Magento 2 Create Invoice programmatically we need following code at
/app/code/onlinecode/CreateInvoice/Observer/CreateSalesOrderInvoicePay.php

<?php
// Magento 2 Create Invoice programmatically
// namespace 
namespace onlinecode\CreateInvoice\Observer\Sales;
// use Observer
use Magento\Framework\Event\Observer as EventObserver;
// use ObserverInterface
use Magento\Framework\Event\ObserverInterface;
// define class
class CreateSalesOrderInvoicePay implements ObserverInterface
{   
	/**
	* @param EventObserver $observer
	* @return $this
	*/
	// define function execute
	public function execute(EventObserver $observer)
	{
		 $invoice_data = $observer->getEvent()->getInvoice();
		 $order_data = $invoice_data->getOrder();
	   
		 /* reset base_total_paid and total_paid of order */
		 $order_data->setTotalPaid($order_data->getTotalPaid() - $invoice_data->getGrandTotal());
		 $order_data->setBaseTotalPaid($order_data->getBaseTotalPaid() - $invoice_data->getBaseGrandTotal());
	}    
}

You also like Get Order Details using Order and customer registration programmatically and Magento2 admin login user detail and Magento 2 Featured products

Leave a Comment

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

71  +    =  81

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US