Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited title
Link
blue
  • 128
  • 5
  • 27

Magento 2: sales_order_save_after called twice after place an order

Source Link
blue
  • 128
  • 5
  • 27

Magento 2: sales_order_save_after called twice after place

I'm using Magento 2.3.4 and tried to catch the data order using sales_order_save_after observer like this:

etc/events.xml

<event name="sales_order_save_after">
    <observer instance="Vendor\Module\Observer\Sales\OrderSaveAfter" name="vendor_module_observer_sales_ordersaveafter_sales_order_save_after"/>
</event>

Vendor\Module\Observer\Sales\OrderSaveAfter

public function execute(
    \Magento\Framework\Event\Observer $observer
) { 
  $order = $observer->getEvent()->getOrder();
  if ($order && $order->getId()){
    $origState = $order->getOrigData('state');
    $curState = $order->getData('state');
    $oldStatus = $order->getOrigData('status');
    $newStatus = $order->getStatus();

    $this->_helper->customLog('OrderSaveAfter Observer', 'test.log');
    $stateLog = 'origState: '.$origState.';'.'curState: '.$curState;
    $statusLog = 'origStatus: '.$oldStatus.';'.'curStatus: '.$newStatus;
    $this->_helper->customLog('State Log => '.$stateLog, 'test.log');
    $this->_helper->customLog('Status Log => '.$statusLog, 'test.log');
  }
  return $this;
}

but i noticed , it logs the same things twice after place an order, is there a way to prevent this?