I want to load the data of an order with the incremental id after placing an order. I am getting the incremental id in the data array after placing an order, but cant load any orders with it.

My Observer:

public function execute(\Magento\Framework\Event\Observer $observer)
        $order = $observer->getEvent()->getOrder()->getData();
        $incrementId = $order['increment_id'];


public function writeShp($incrementId){
        try {
            $order = $this->orderFactory->create()->loadByIncrementId($incrementId);
            $this->logger->info('order', array('order', $order));
        } catch (\Exception $e) {

The following is getting logged:

[2019-03-07 14:54:02] main.INFO: 000000040 [] []
[2019-03-07 14:54:02] main.CRITICAL: No such entity with orderId =  [] []

The weird thing is the [ ] [ ] behind the incrementId. Could this be the problem?

I have tried this:

$onlyId = preg_replace("/[^0-9]/", '', $incrementId);

but still there would be [ ] [ ] behind the incrementid. Imploding also didn't work, it said it wasnt an array.

  • which event do you use? sales_order_place_after? – HelgeB Mar 7 '19 at 15:56
  • Yes that's the one! Dont want to use the checkout_onepage_succes one since not all payment methods lead to that page – Digitaq Mar 7 '19 at 16:06

In the event sales_order_place_after the order you try to load isn't stored in the database at that moment. That's why loadByIncrementId() fails. The save() call comes right after that event.

So you can either take the $order object as it will be saved, that's the parameter passed to the observer or take a later event like sales_order_save_after or even sales_order_save_commit_after if you really want to load the data from database (for what ever reason). In both cases you will have for example the the order id.

| improve this answer | |
  • Totally forgot! sales_order_save_after might be a better option then, but that will also get fired when an admin saves an order after it's already created right? Thats not what I want since the observer should only get fired once after an order is created. – Digitaq Mar 7 '19 at 16:21
  • I don't know your exact requirement but for example you can place the observer in the frontend area - in that case you will not touch any order from admin. But anyway sales_order_save_after might be triggered more than once in the order's lifetime even in frontend. But I'm sure you should better use that event. – HelgeB Mar 7 '19 at 16:25

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.