2

I'm implementing extension for Magento 2. I've been trying to add observer for event sales_order_save_commit_after but for some reason the observer doesn't catch the event. I've added logging for the events based on these instructions: How to get events/observers in magento 2 and it seems the event gets fired but is not catched by the observer.

Here's the frontend/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_commit_after">
        <observer name="my_dummy_observer" instance="My\Plugin\Model\Observers\Dummy" />
    </event>
</config>

And here's the observer

use Psr\Log\LoggerInterface;

class Dummy implements ObserverInterface
{
    protected $_logger;

    public function __construct(
        LoggerInterface $logger
    ) {
        $this->_logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $event = $observer->getEvent()->getName();
        $this->_logger->info("\n" . '[OBSERVER EVENT CATCHED]: ' . $event . "\n");
    }
}

Any idea why the event is not catched with that configuration?

  • Are you missing a namespace declaration at the top of the class file or have you just forgotten to paste it in here? – Smartie Feb 16 '16 at 11:18
  • The namespace is in the actual source file. The observer itself works fine if a hook it for some common event like catalog_product_collection_load_after – Hannu Feb 16 '16 at 11:39
3

I figured out this one by myself. Apparently you cannot catch this event (sales_order_save_commit_after) from frontend (app/code/My/Plugin/etc/frontend/events.xml) but you need to have the observer defined in "root" level (app/code/My/Plugin/etc/events.xml)

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.