Magento – sales_order_place_after not triggering in magento 2

event-observermagento2.1.5sales-order

I have created etc/events.xml as:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->

<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="process_gateway_redirect" instance="Magento\SamplePaymentGateway\Observer\ProcessGatewayRedirectOrderPlaceAfter" />
   </event>
</config>

And observer for it:

<?php
namespace Magento\SamplePaymentGateway\Observer;

use Magento\Framework\Event\ObserverInterface;

class ProcessGatewayRedirectOrderPlaceAfter implements ObserverInterface
{
    protected $orderFactory;

    public function __construct(\Magento\Quote\Model\QuoteFactory $quoteFactory,
    \Magento\Sales\Model\Order $orderFactory)
    {
        $this->orderFactory = $orderFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo 'here I\'m';
        $orderIds = $observer->getEvent()->getOrderIds();
        $lastorderId = $orderIds[0];

        $order = $this->orderFactory->load($lastorderId);
        var_dump($order);
        die();
    }
}

But the event is not executing, one page success is shown.
I even tried creating the etc/frontend/events.xml with same code. Anyway the event is not triggered.

I have found that many people are using this event and discussing about other kind of issues.

Many friends are suggesting me to use checkout_submit_all_after and checkout_onepage_controller_success_action. I know about these events and have used previously. But my question is still the same.

Is it impossible to catch the sales_order_place_after?

Best Answer

sales_order_place_after is not proper,bcoz of it is not fire every time.

sales_order_place_after naturally fires whenever call place() of Magento\Sales\Model\Order and it's call depend on payment method.

As suggestion, iam suggestion to you use

checkout_submit_all_after

It will fire both for frontend /admin order