I'm currently working on a custom module. I need to get order data of the order that has just been placed. The problem is, that I cannot get the data with Magento\Sales\Model\Order. It looks like the event is triggered too soon to use the Order Model. I've tried a couple of events but none seem to work. Does anyone know a solution that will work? It may be a different event, an alternative for the Order Model, or any fixes in the code.



namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order;

class OrderExport implements ObserverInterface {

    protected $_logger;

    protected $orderModel;

    public function __construct(
        LoggerInterface $logger,
        Order $orderModel
    ) {
        $this->_logger = $logger;
        $this->orderModel = $orderModel;

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order = $observer->getEvent()->getOrder();

        $orderId = $order->getIncrementId();
        $orderId = ltrim($orderId, '0');
        $orderId = (int)$orderId;

        $this->_logger->info("New order: ".$orderId);


    public function createXml($orderId) {
        $orderModel = $this->orderModel;

        // It breaks on this:
        $customerName = $orderModel->getBillingAddress()->getFirstname();


<?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="order_export" instance="Vendor\Module\Observer\OrderExport" />

1 Answer 1


First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.

If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).

If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.

  • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.
    – MartinE
    Mar 26, 2019 at 10:35
  • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.
    – MartinE
    Mar 26, 2019 at 12:06

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.