Magento – Order confirmation observer in Magento 2 not working

event-observermagento2orders

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?

Best Answer

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)