1

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 (As I thought It will work in magento 2 because it is work fine in magento 1.x).

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

Please help.

4

--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

  • hi I am getting unknown function getOrder() i am using event and obsertver – Sarvesh Tiwari Nov 8 '17 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.