How to get Order No. at event sales_order_place_after using event-observer model?

How to get Order No. at event sales_order_place_after using event-observer model?

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get Order No. at event sales_order_place_after using event-observer model?

Getting Order No. via sales_order_place_after Event Using Observer Model

As an experienced Magento developer, I often encounter scenarios where clients need to access order details immediately after the checkout process is completed. One common requirement is to get the order number right away and pass it to an external PHP file for further processing.

In your case, you’re using the sales_order_place_after event but struggling with accessing the increment ID in your external Function1.php. Let me guide you through this step-by-step.

Understanding sales_order_place_after Event

This event is triggered immediately after an order is placed by a customer. It’s part of the Magento observer pattern, allowing you to hook into key processes for customization.

Your current Observer.php code looks good; you’re successfully fetching the order ID and increment ID from the event object.

public function Mytestmethod($observer) {
    $incrementid = $observer->getEvent()->getOrder()->getIncrementId();
}

Accessing Order Data

To access more data, like the order number or other attributes, you can use Magento’s built-in getters:

$order = $observer->getEvent()->getOrder();
$orderNumber = $order->getIncrementId(); // Increment ID (unique order number)
$orderDate = $order->getCreatedAt();   // Order creation date

Including External PHP File

Now, to include your external Function1.php file:

  1. Place Function1.php in the same directory as your Observer.php or a subdirectory.

  2. Use the following syntax to include it at the beginning of your Observer.php:

require_once('path/to/your/Function1.php');

For example, if Function1.php is in a subdirectory called ‘connection’:

require_once('connection/Function1.php');

Passing Data to External File

You can pass data from the observer to your external file using class methods. Create a method that accepts an argument (like the increment ID) in Function1.php:

class testDatabase {
    public function processOrder($incrementId) {
        // Your processing logic here, e.g., database insertion
        $this->insertIntoDatabase($incrementId);
    }

    private function insertIntoDatabase($incrementId) {
        // Database connection and order ID insertion logic
    }
}

Then, in your Observer.php:

$orderNumber = $observer->getEvent()->getOrder()->getIncrementId();
$processor = new testDatabase();
$processor->processOrder($orderNumber);

Conclusion

In summary, to get the order number at sales_order_place_after event using an observer model and pass it to an external PHP file:

  1. Fetch the order data in your observer method.
  2. Include the external file correctly.
  3. Create a method in the external file that accepts the necessary arguments.
  4. Call this method from the observer with the required data.

Remember, always test your code thoroughly after making changes to ensure no conflicts or errors arise during the checkout process.

For more advanced customizations and best practices, I recommend checking out our comprehensive guides on Magento event-observers at Laravel Company.

Checkout Extensions Orders Customers

Our network