Magento 2 how to get order id on admin order invoice save?
-
14-04-2021 - |
Domanda
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 ?
Soluzione
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;
}
}
Altri suggerimenti
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;
}
}
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();
}
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange