Magento – Not getting invoice id in after “sales_order_invoice_save_after” event magento2

event-observerinvoicemagento2

I am trying to save invoice info in our custom table. So I tried "sales_order_invoice_save_after and sales_order_invoice_pay" events to get invoice data just after creation.

But Observer file didn't getting invoice data, i m using below code in our observer file –

namespace Customcode\Productserialno\Observer\Sales;
class InvoiceSaveAfter implements \Magento\Framework\Event\ObserverInterface
{

    protected $logger;
    protected $_objectManager;


    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\ObjectManagerInterface $objectManager
    ){
            $this->logger = $logger;
            $this->_objectManager = $objectManager;
    }


    public function execute(EventObserver $observer)
    {
        try{
            echo "Here in observer";
            $invoice = $observer->getEvent()->getInvoice();
            $order = $invoice->getOrder();
            echo "<pre>"; 
            print_r($invoice->getOrigData());
            echo $invoiceId = $observer->getEvent()->getInvoice()->getOrigData('entity_id');
            echo "<pre>"; echo "E";
            if($invoiceId == ''){
                echo "Not found"; 
            }

             die;
        } catch (\Exception $e) {
            echo $e->getMessage(); die;
            $this->logger->debug($e->getMessage());
        }
    }
}

But its not returing any invoice related data, even I checked database there is also not made any new invoice related entry.

Please let me know, what I am doing wrong? Is there is some other "event" I need to use ?

Best Answer

I have a similar requirement, I need to get invoice information after invoice save from admin.
I fulfil the requirement using the event observer.
app/code/Anshu/Customization/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_postdispatch_sales_order_invoice_save">
        <observer name="observer_admin_invoice_save" instance="Anshu\Customization\Observer\AnshuInvoiceSave" />
    </event>
</config>

app/code/Anshu/Customization/Observer/AdminInvoiceSave.php

<?php

namespace Anshu\Customization\Observer;

use Magento\Framework\Event\ObserverInterface;

class AnshuInvoiceSave implements ObserverInterface 
{
    /**
    * @var \Magento\Framework\Registry
    */

   protected $_registry;

    public function __construct(
        \Magento\Framework\Registry $registry
    )
    {
        $this->_registry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        $invoice = $this->getInvoiceObject();
        // My Customization
    }

    private function getInvoiceObject()
    {
        return $this->_registry->registry('current_invoice');
    }

} 

Check if it is helpful to you.
Magento version was 2.2.1

Related Topic