0

I created an observer for changing the shipping method amount but it does not work. This is my code

Cop/Postexpres/etc/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="popopp" instance="Cop\Postexpres\Model\Observer"  />
</event>

and

Cop/Postexpres/Model/Observer.php

 namespace Cop\Postexpres\Observer;
      use Magento\Framework\Event\ObserverInterface;

class Observer implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        $order = $shipment->getOrder();
       var_dump($order->getData());

    }

}
1

It looks like you passed wrong instance in your events.xml. You passed

<observer name="popopp" instance="Cop\Postexpres\Model\Observer"  />

and your namespace Observer is

namespace Cop\Postexpres\Observer;

So, you just need to add correct namespace(you are missing Model in your namespace).

| improve this answer | |
2

Try following code:

  1. app/code/[VendorName]/[ModuleName]/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_save_after" instance="
            [VendorName]\[ModuleName]\Observer\ProcessShipment" />
    </event>
</config>
  1. app/code/[VendorName]/[ModuleName]/Observer/ProcessShipment.php
<?php
namespace [VendorName]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;

class ProcessShipment implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        $order = $shipment->getOrder();
    }
}
| improve this answer | |
  • Thanks .... i copy you code and replace company and module name ... and compile and flush cache ... but it doesn't worked – gh darvishani Jan 21 '18 at 7:54
  • 1
    @ghdarvishani - Maybe event "sales_order_shipment_save_after" is not available in M2. – Pratik Oza Jan 21 '18 at 11:16
  • omg :( ... how i can check it ... i found this event after many search – gh darvishani Jan 21 '18 at 11:19
  • You can check using command: find . -type f -exec grep -n -H -A 2 -T "eventManager->dispatch(" {} \; – Pratik Oza Jan 21 '18 at 11:24
  • 1
    I can confirm that sales_order_shipment_save_after event is available in M2. I tested it and it works without any problem. @PratikOza – Keyur Shah Jan 21 '18 at 14:32
1

Your namespace is incorrect. In your observer class, namespace should be

Cop\Postexpres\Model\Observer

instead of

Cop\Postexpres\Observer

Rest everything is fine!

| improve this answer | |

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.