0

I have created an observer but it does not seem to ever fire - I need to replicate sales_order_place_after from an old Magento 1.9.X module into the one I am refactoring for Magento2

So far I have \MyVendor\Order\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="checkout_onepage_controller_success_action">
        <observer name="MyObserver" instance="MyVendor\Order\Observer\OrderObserver" />
    </event>
</config>

And also OrderObserver.php which is:

<?php

namespace MyVendor\Order\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Psr\Log\LoggerInterface;

class OrderObserver implements ObserverInterface
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
        parent::_construct();
        //Observer initialization code...
        //You can use dependency injection to get any class this observer may need.
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //Observer execution code...
        $this->logger->debug('Order Observer');
    }
}

I've tried moving the events.xml into a sub folder of etc for frontend, but I seem unable to reach my debug log. What have I done that needs amending to get this observer to fire?

1 Answer 1

0

Looks Like you messed up with directory structure that's why its not working for you.

Below is tested with Magento Instance.

MyVendor\Order\etc\frontend\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="checkout_onepage_controller_success_action">
    <observer name="MyObserver" instance="MyVendor\Order\Observer\OrderObserver" />
</event>
</condfig>

MyVendor\Order\Observer\OrderObserver.php

namespace MyVendor\Order\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class OrderObserver implements ObserverInterface
{
  public function execute(Observer $observer)
  {
      echo "Observer Executed"; exit;
  }
}
7
  • Hi - I did say that I had tried it in a subfolder for frontend but that also did not work. Otherwise I cannot see a difference in your structure to mine.
    – Aravona
    Jun 26, 2018 at 7:41
  • I used same code, As I posted above its working for me. Please verify that you have created necessary files for basic module.
    – TBS Mage
    Jun 26, 2018 at 7:58
  • Yes there is a module in place, and other aspects of the module are working.
    – Aravona
    Jun 26, 2018 at 8:03
  • Can't help with that, Its working for me as above code.
    – TBS Mage
    Jun 26, 2018 at 8:05
  • Even if you log rather than echo and exit?
    – Aravona
    Jun 26, 2018 at 8:43

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.