I have created a custom shipping module, it’s possible to make orders with it.
Now I need to figure out how to hook on to the saving of a delivery of the order.
I basically need to run custom code when admin have created a shipment from the order.
How do you do that?
The \Magento\Sales\Model\Order\Shipment
class, as any other \Magento\Framework\Model\AbstractModel
descendant, fires some events on saving:
/**
* 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;
}
/**
* Processing object after save data
*
* @return $this
*/
public function afterSave()
{
$this->cleanModelCache();
$this->_eventManager->dispatch('model_save_after', ['object' => $this]);
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
$this->_eventManager->dispatch($this->_eventPrefix . '_save_after', $this->_getEventData());
$this->updateStoredData();
return $this;
}
Use them to run your custom PHP code.
1 Like