1

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 ?

  • Is your function called? – Nikolas Aug 31 '17 at 16:37
0

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

  • not working if you create the invoice through API – Franck Garnier May 21 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. – Anshu Mishra May 21 at 4:53
  • It is a good workaround for admin. – Franck Garnier May 22 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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