浏览文章

文章信息

Magento2 销售订单发货事件后保存事件|Sales Order Shipment Save After event 16469

- 销售订单发货事件后保存:

app/code/Company/Module/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="sales_order_shipment_save_after">
        <observer name="sales_order_shipment_after"
                  instance="Company\Module\Observer\SalesOrderShipmentAfter" />
    </event>
</config>


app/code/Company/Module/Observer/SalesOrderShipmentAfter.php

<?php
namespace Company\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class SalesOrderShipmentAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        /** @var \Magento\Sales\Model\Order $order */
        $order = $shipment->getOrder();
        // Do some things

    }
 }
?>


--覆写 Controller

我们可以使用插件或首选项。这里使用后者。

app/code/Company/Module/etc/adminhtml/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"
    <preference for="Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save"
                type="Company\Module\Controller\Adminhtml\Order\Shipment\Save" />
</config>


app/code/Company/Module/Controller/Adminhtml/Order/Shipment/Save.php

<?php
namespace Company\Module\Controller\Adminhtml\Order\Shipment;
class Save extends \Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save
{

}
?>

涉及到di.xml请执行

bin/magento setup:di:compile


原创