I'm trying to get order data (order id for now) from an observer using the event sales_order_place_after.
The event fires and does not get any errors, but my order_id is blank. Any help as to why this isn't working as I expect?
My 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="PingOnSale" instance="Vendor\Module\Observer\PingOnSale" /> </event> </config> My Observer
namespace Vendor\Module\Observer; use Magento\Framework\Event\ObserverInterface; class PingOnSale implements ObserverInterface { /** * @param \Magento\Framework\Event\Observer $observer * @return bool */ public function execute(\Magento\Framework\Event\Observer $observer){ $order = $observer->getData('order'); $order_id = $order->getId(); // $order_id is blank when logging to file return true; } } I have also tried this method, that is the accepted answer from the question which did not work. According to the documentation, $observer->getData(); is the right way to do this.
The event dispatches from Magento\Sales\Model\Order and as far as I can tell my code should be getting the order. Here is the relevant code from the Order class.
public function place() { $this->_eventManager->dispatch('sales_order_place_before', ['order' => $this]); $this->_placePayment(); $this->_eventManager->dispatch('sales_order_place_after', ['order' => $this]); return $this; }