2

I am tring to figure out when sales_order_save_before is dispatched but I do not see it in the source.

What class is responsible for dispatching this event?

2 Answers 2

1

The event sales_order_save_before is been dispatched when an entity of Sales Order is been saved.

The class which is reponsible for dispatching sales_order_save_before event is the model of order Magento\Sales\Model\Order. In order to get to the dispatch statement, you need to look for the beforeSave method into inheritance heirarchy of this model. If you do so, you will end up in Magento\Framework\Model\AbstractModel class which contains the save, beforeSave and afterSave methods.

public function beforeSave()
{
     if (!$this->getId()) {
         $this->isObjectNew(true);
     }
     $this->_eventManager->dispatch('model_save_before', ['object' => $this]);
     $this->_eventManager->dispatch($this->_eventPrefix . '_save_before', $this->_getEventData());
     return $this;
 }

Just above the return statement in beforeSave method the eventManager is dispatching this event by concating the eventPrefix which is sales_order for Order entity declared in its Model class.

2
  • Thank you for the full explanation. Marking yours as correct I do not want to mark my answer as correct.
    – Logic4Life
    Commented Nov 26, 2019 at 3:35
  • Thanks Man @logic4life Commented Nov 26, 2019 at 7:05
1

I have found that this event are dispatched in Magento\Framework\Model\AbstractModel.php

    /**
     * Processing object before save data
     *
     * @return $this
     */
    public function beforeSave()
    {
        if (!$this->getId()) {
            $this->isObjectNew(true);
        }
        $this->_eventManager->dispatch('model_save_before', ['object' => $this]);
        $this->_eventManager->dispatch($this->_eventPrefix . '_save_before', $this->_getEventData());
        return $this;
    }

As you can see its dynamicly created at this line

$this->_eventManager->dispatch($this->_eventPrefix . '_save_before', $this->_getEventData());

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.