How To

How to Get Order Status in sales_order_place_after event in Magento 2

Ordering and receiving products at home are easy and look simple but it is not as easy as you think for the store owner. Because once customer place an order, it process and passes through different Magento 2 order Management statuses like New Order, Pending, Payment Processing, On Hold, Complete, Canceled. But while developing or custom coding, sometimes we need to fetch current order status like payment is successfully done or not? or to fetch order state for creating or setting up further rules and conditions to fulfill business requirements.

Here we back with a small piece of code to get Order status in sales_order_place_after event.
To do the same first you need to create observer into events.xml

app\code\Vendor\Extension\etc\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="vendor_sales_order_place_after" instance="Vendor\Extensoin\Observer\OrderObserver" />
</event>
</config>

After that, you need to create Observer file at following location.
app\code\Vendor\Extension\Observer\OrderObserver.php

<?php
namespace Vendor\Extension\Observer;
 
use Magento\Framework\Event\ObserverInterface;
 
class OrderObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
         $statuscode = $observer->getEvent()->getOrder()->getStatus();
         $statuslabel = $observer->getEvent()->getOrder()->getStatusLabel();
     }
}

You can use this code according to your need of getting order status anywhere inside custom development or custom extension.

Lastly, smash that stars if the code worked for you and don’t forget to comment down below if you are looking for any help regarding this code.

Happy Coding.

Click to rate this post!
[Total: 42 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago