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());

    }

}

3 Answers 3

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

19
  • oh sorry ... i was wrong about copy ... but in my code all namesapace and instance are true ...i was check it for sevral time Jan 21, 2018 at 9:34
  • 1
    Post your correct code here so peoples don't add as answer :)
    – Keyur Shah
    Jan 21, 2018 at 9:36
  • paste.ubuntu.ir/ucln paste.ubuntu.ir/fmgd app/code/Netweb/Postexpres/etc/events.xml app/code/Netweb/Postexpres/Model/Observer.php Jan 21, 2018 at 10:18
  • 1
    And please also post your observer code @ghdarvishani
    – Keyur Shah
    Jan 21, 2018 at 10:28
  • 1
    For temp log you can use this answer magento.stackexchange.com/a/92441/2589 it is very easy to debug
    – Keyur Shah
    Jan 21, 2018 at 10:49
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();
    }
}
8
  • Thanks .... i copy you code and replace company and module name ... and compile and flush cache ... but it doesn't worked Jan 21, 2018 at 7:54
  • 1
    @ghdarvishani - Maybe event "sales_order_shipment_save_after" is not available in M2.
    – Pratik Oza
    Jan 21, 2018 at 11:16
  • omg :( ... how i can check it ... i found this event after many search Jan 21, 2018 at 11:19
  • You can check using command: find . -type f -exec grep -n -H -A 2 -T "eventManager->dispatch(" {} \;
    – Pratik Oza
    Jan 21, 2018 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, 2018 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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