3

I want to catch sales order place after event in magento 2.

Here is my 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="neo_ordercomment_observer_orderneo" instance="Neo\OrderComment\Observer\Orderneo" />
   </event>


</config>

And here is my Orderneo file, which is the observer

class Orderneo implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
     $order= $observer->getData('order');
     $comment = $this->getRequest()->getParams('comment');
     print_r("Catched event succssfully !"); exit;
    }
}

But it is not comming to this file,

It should print that last line and then exit. But it is redirecting to magento's order success page.

  • 1
    Use checkout_onepage_controller_success_action event – Ankit Shah Mar 29 '17 at 9:26
  • Okay means in events.xml, do I have to replace the event name ? – Mrunmay Deswandikar Mar 29 '17 at 9:27
  • 1
    Yes <event name="checkout_onepage_controller_success_action"> & public function execute(\Magento\Framework\Event\Observer $observer) { print_r("Catched event succssfully !"); exit; } – Ankit Shah Mar 29 '17 at 9:29
  • Okay let me check.. – Mrunmay Deswandikar Mar 29 '17 at 9:29
7

You can try with below code in your observer and check,

class Orderneo 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;
    }
}
  • So no need to make changes, in events.xml ? – Mrunmay Deswandikar Mar 29 '17 at 9:32
  • 1
    yes no need to change in event.xml file – Rakesh Jesadiya Mar 29 '17 at 9:34
  • No, still directly order success page is getting called – Mrunmay Deswandikar Mar 29 '17 at 9:42
  • 1
    You can try with @ankit suggested event, checkout_onepage_controller_success_action and check. – Rakesh Jesadiya Mar 29 '17 at 9:43
  • 1
    just comment line $comment = $this->getRequest()->getParam('comment'); and check. – Rakesh Jesadiya Mar 29 '17 at 9:55

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.