0

I have done this so for:

app\code\Vendor\Extension\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_place_after">
<observer name="vendor_sales_order_place_after" instance="Vendor\Extensoin\Observer\SendEmail" />
</event>
</config>

And at: app\code\Vendor\Extension\Observer\SendEmail.php

namespace Vendor\Extension\Observer;

class SendEmail implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
 $order = $observer->getEvent()->getOrder();
 echo $orderId = $order->getId();
 $comment = $this->getRequest()->getParam('comment');
 print_r("Catched event succssfully !"); exit;
}
}

But noting happens and I go to success page as usual. How to know event is captured or is there some other way to achieve it?

1
  • The observer class defined in your xml file does not match with your observer class name Nov 30, 2018 at 6:40

1 Answer 1

2

seems your observer class name is wrong SendEmail

your events.xml looks like

<?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_save_after">
        <observer name="mymodule_sales_order_save_after" instance="MyCompany\MyModule\Observer\Observer" />
    </event>
</config>

your observer file looks like

    <?php namespace MyCompany\MyModule\Observer; 
    use Magento\Framework\Event\ObserverInterface; 

    class Observer implements ObserverInterface { 

        protected $connector; public function __construct() { 
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        }

        public function execute(\Magento\Framework\Event\Observer $observer) { 
            $order = $observer->getEvent()->getOrder();
            $customerId = $order->getCustomerId();
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info('Catched event succssfully');

       }
   }
8
  • Thanks @Hitesh, do confirm me is it event.xml or events.xml? secondly where the responce goes to show event is captured.. right now trying this answer it is still going to success page like in normal checkout process.
    – Ajwad Syed
    Nov 30, 2018 at 6:13
  • it's events.xml typo, did you try my code?
    – Hitesh
    Nov 30, 2018 at 6:16
  • Let me try this again
    – Ajwad Syed
    Nov 30, 2018 at 6:19
  • Still same.. I tried echo "<pre>"; print_r("Captured Successfuly"); exit;
    – Ajwad Syed
    Nov 30, 2018 at 6:29
  • echo exist will not work there I think to check my updated answer, try to log it
    – Hitesh
    Nov 30, 2018 at 6:30

Your Answer

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

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