2

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 ?

1
  • Is your function called?
    – Nikolas
    Aug 31, 2017 at 16:37

1 Answer 1

2

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

3
  • not working if you create the invoice through API May 21, 2019 at 4:39
  • @FranckGarnier Yes, this will not work if you create invoice through API as I have used the event controller_action_postdispatch_sales_order_invoice_save which is fired when we create invoice through admin-end. May 21, 2019 at 4:53
  • It is a good workaround for admin. May 22, 2019 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.