2

I need to call some API on ship item from Magento but nothing is working.

Following things I already tried:

  1. Use event/observer: sales_order_shipment_save_after. I thought this would work in Magento 2 because it did in Magento 1).

  2. Override controller: \Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save but controller overriding is also not working.

1

2 Answers 2

7

--Sales Order Shipment Save After event:

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

    }
}

--Override controller

We can use Plugin or preference.

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
{

}

Make sure that you clean Magento Cache and remove var/generation

2
  • hi I am getting unknown function getOrder() i am using event and obsertver Commented Nov 8, 2017 at 7:53
  • hello @Khoa, I am using this event and trying to get source_code from "inventory_shipment_source" table, but in this event data not present in the table for current shipment_id Commented Nov 4, 2020 at 11:42
0

Handles synchronous insertion of the new entity into corresponding grid on certain events.

Used in the next events:

  • sales_order_save_after
  • sales_order_invoice_save_after
  • sales_order_shipment_save_after
  • sales_order_creditmemo_save_after

Works only if asynchronous grid indexing is disabled in global settings.

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.