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
app/code/Anshu/Customization/Observer/AdminInvoiceSave.php
Check if it is helpful to you.
Magento version was 2.2.1