1

I'm new to Magento and the next step I want to learn is to listen to an event. So, I'm trying to find which event should I trigger that works after a customer has placed an order that also carries that order's information, including product SKUs, Order number, etc.?

1

I'm using Magento 2.3.x and 2.4.x and I use this event: sales_order_save_commit_after

1

I get most informations I need from a order by using checkout_onepage_controller_success_action You get the orderid, customer informations and the item informations.

Just as an addition: I tried multiple observers - most of them have one or two problems: Either, the informations are not in the database, when the trigger gets triggered or it gets triggered each time, an order is saved (so it gets triggered multiple times, even after the checkout process is done already).

1

Events in Magento 2 are dispatched based on an action performed and it passes data to the observer. Observers are Magento classes that are executed when an event is dispatched. Here, we will make use of this concept to get order data from Magento 2

“sales_order_place_after” event.

Step 1: Create events.xml file at app/code/Vendor/Extension/etc/

<?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="place_order_after" instance="Vendor\Extension\Observer\Orderplaceafter"/>
    </event>
</config>

Step 2: Create Orderplaceafter.php at app/code/Vendor/Extension/Observer/

namespace Vendor\Extension\Observer;
 
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
 
class Orderplaceafter implements ObserverInterface
{
    protected $logger;
 
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
 
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $order = $observer->getEvent()->getOrder();
            print_r($order->getData());die;
        } catch (\Exception $e) {
            $this->logger->info($e->getMessage());
        }
    }
}

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.