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 my observer file is Orderneo file,

<?php

namespace Neo\OrderComment\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.

  • 2
    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
0

You can use checkout_onepage_controller_success_action and sales_order_save_after event while after order placing event.

But you can not use print_r(),die(); and exit(); for debugging. So you can use log method for debugging here.

you can use print_r(),die();,exit(); and also log method in checkout_onepage_controller_success_action event.

while useing checkout_onepage_controller_success_action event

Filepath:app\code\Vendorname\Modulename\etc\frontend\events.xml

<event name="checkout_onepage_controller_success_action">
    <observer name="mymodule_controller_success_action" instance="Vendorname\Modulename\Observer\MyObserver"  />
</event> 

Filepath:app\code\Vendorname\Modulename\MyObserver.php

<?php
namespace Vendorname\Modulename\Observer;

use Magento\Framework\Event\ObserverInterface;

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

}

while using sales_order_save_after event

Filepath:app\code\MyCompany\MyModule\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="sales_order_save_after">
        <observer name="mymodule_sales_order_save_after" instance="MyCompany\MyModule\Observer\MyObserver" />
    </event>
</config>

Filepath:app\code\MyCompany\MyModule\MyObserver.php

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

    class MyObserver implements ObserverInterface { 


        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');

       }
   }

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.