2

so I've been looking to do some adjustments with an observer by listening to the event that is fired after a user successfully places an order, whether in the backend or the frontend. But, I don't know which event I'm looking for. My best guesses are:

  1. checkout_submit_all_after
  2. checkout_controller_onepage_saveOrder I got the above events from This list of Magento 2 events

Can someone help me understand which event I need? and which of the ones I provided, if any,is correct and why? and what's the difference between both?

2 Answers 2

3

sales_order_place_after

Below is the sample code of the and event which is trigered when order is placed. 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="my_event_name" instance="Foo\Bar\Observer\MyObserver" />
    </event>
</config>

MyObserver.php

<?php
namespace Foo\Bar\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        /** @var $orderInstance Order */
        $order = $observer->getOrder();
        $orderId = $order->getRealOrderId();

        \Zend_Debug::dump($orderId);
        die();
    }
}
4
  • Thank you so much for the detailed answer. ill use this code and if it works ill mark your answer as the correct one.
    – Zyzzx
    Dec 7, 2021 at 9:30
  • yes let me know if you need any more help
    – Pramod
    Dec 7, 2021 at 13:22
  • I adjusted my question. I noticed that I wasn't being very clear about which event I wanted. And I was confused about only one thing in your code which is "/** \@var $orderInstance Order **/" can you tell me which class I need to import so that I can check its methods.
    – Zyzzx
    Dec 7, 2021 at 13:32
  • that code was just a sample, you can add your own logic in execute method
    – Pramod
    Dec 7, 2021 at 13:36
3

sales_order_place_after is fire when place() of order object is called and in magento every payment does not call place() when order placed.


Where

checkout_onepage_controller_success_action 

Is only called when customer has been redirect to checout/onepage/success. Also this event only called whenever order place from frontend and customer visit checout/onepage/success.

MY suggestion, this two event not proper,We should be use checkout_submit_all_after this event is fire when order place from frontend and backend.

it is fire immediate order place

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.