catch order place after event magento2
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;
}
}
better use this event instead of sales_order_save_after and checkout_onepage_controller_success_action
checkout_submit_all_after.
it will work even site not redirected to success page because of some errors like internet issue and all.
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');
}
}