2

I am using sales_order_place_after event in Magento 2 inside Namespace/Module_name/etc/. In observer, I am getting order increment id by unable to get order id and created date of Order.

Example:

$order = $observer->getEvent()->getOrder();

$orderId = $order->getId();   

$createdAt =  $order->getCreatedAt();

I am unable to get both order id and created date.

Please help me in this?

5 Answers 5

4

Try using following event:

checkout_submit_all_after

Observer Code :

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class CheckoutAllSubmitAfterObserver implements ObserverInterface
{
    /**
     *
     * @param Observer $observer
     * @return $this
     */
    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        $orderId = $order->getId();

        $createdAt =  $order->getCreatedAt();

        return $this;
    }
}
1
  • this method will not work, because checkout_submit_all_after event is dispatched only after placing order via magento customer interface. The problem is that order can be placed via magento api or created programmically in system
    – Max
    Jan 10, 2017 at 16:28
2

Event sales_order_place_after triggered after payment placing. There is no stored order at this moment. OrderManagementInterface save order entity in database after this, therefore there are no db-generated fields such as created_at and entity_id. Use plugin on \Magento\Sales\Api\OrderManagementInterface::place.

2

I used sales_order_save_after event instead of sales_order_place_after in my event.xml file and now its working fine. I am getting both Order Id and Created Date also.

5
  • this method will not work, because sales_order_save_after event is automatically dispatched after any saving of order entity. thus you will catch this event not only after order placing. Moreover, your method violate service layers principle. I suggested the solution in answers.
    – Max
    Jan 10, 2017 at 16:25
  • try to add comment to your order or change status, your observer will be called again after saving order
    – Max
    Jan 10, 2017 at 18:41
  • Yes , You are right @MaxStsepantsevich , but i want this through event and observer only. how to achieve this.
    – BornCoder
    Jan 10, 2017 at 18:42
  • there are no impediments to create plugin instead of observer, am i right?
    – Max
    Jan 10, 2017 at 18:47
  • @Max Can you please suggest event which will fire only one time after successfully placing order. It should also fire when placing order from admin screen. I'm using "checkout_onepage_controller_success_action" which is work well at frontside but not working at admin side Apr 28, 2017 at 13:54
1

Its always good to use checkout_submit_all_after observer event for admin and frontend logic so as to get the order data when the order is successfully placed.

0

order_place_after when executed does not save the data in database that is why you are not getting the order_id for the same. Instead try using sales_order_save_after

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.