0

How to get order id in invoice using plugin ?

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Controller\Adminhtml\Order\Invoice\Save">
        <plugin name="admin_plugin_order_invoice_save_after" type="Companyname\Modulename\Controller\Adminhtml\Order\Invoice\Save"/>
    </type>
</config>

Save.php

<?php
namespace Companyname\Modulename\Controller\Adminhtml\Order\Invoice;
ini_set('display_errors',1);
error_reporting(E_ALL);

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;

class Save 
{
    public function afterExecute(\Magento\Sales\Controller\Adminhtml\Order\Invoice\Save $subject,$result)
    {
        echo "order id ---".$result->getEntityId();
        echo "<pre>";
        print_r($result->getData());
        
        
        die;
    }
}

Unable to get anything. Using this code unable to get order it or invoice id. Anyone know how to get order id,update order or get invoice id ?

4
  • Do you want invoice id or Order id? Commented Aug 9, 2020 at 16:30
  • @DhirenVasoya order id Commented Aug 10, 2020 at 4:40
  • I post answer, please check if that help you. Commented Aug 10, 2020 at 6:05
  • @DhirenVasoya i used to get order id using registry invoice.Thanks for the answer Commented Aug 10, 2020 at 8:07

3 Answers 3

0

Please change the Save.php as below and you will get the order id

<?php
namespace Companyname\Modulename\Controller\Adminhtml\Order\Invoice;
ini_set('display_errors',1);
error_reporting(E_ALL);

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;

class Save 
{

    /**
     * @var Registry
     */
    protected $registry;

    /**
     * @param Registry $registry
     */
    public function __construct(
        \Magento\Framework\Registry $registry
    ) {
        $this->registry = $registry;
    }

    public function afterExecute(\Magento\Sales\Controller\Adminhtml\Order\Invoice\Save $subject, $result)
    {
        $invoice = $this->registry->register('current_invoice');
        $orderId = $invoice->getOrderId();
        $invoiceId = $invoice->getId();

        return $result;
    }
}
2
  • i also use same using registry but it is safe using invoice registry ? Commented Aug 10, 2020 at 4:42
  • Yes it is safe. You are just using the register data only. You are not modify that. Commented Aug 10, 2020 at 4:56
1

You can get the Order Id in this way.

class Save 
{
    public function afterExecute(\Magento\Sales\Controller\Adminhtml\Order\Invoice\Save $subject,$result)
    {
        $orderId = $subject->getRequest()->getParam('order_id');
        echo $orderId;
    }
}
1
  • upvote for answer Commented Aug 10, 2020 at 8:08
0
  You can Try this
  Venddor/Module/etc/event.xml 

<event name="sales_order_save_after">
  <observer name="reward_ggn" instance="Vendor\Module\Observer\ClassName"/>
</event>

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;


class ClassName implements ObserverInterface 
{

 public function execute(\Magento\Framework\Event\Observer $observer) 
 {      
    $order = $observer->getEvent()->getOrder();

            if($order->getState() == 'complete') {
               
            $order->getOrderId();
            }       
 }
 }
1
  • i want to use with plugin not event Commented Aug 10, 2020 at 4:41

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.