Skip to content

Add Event for sales_order_state_change_before during Order->saveState() #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions app/code/Magento/Sales/Model/Order.php
Original file line number Diff line number Diff line change
@@ -919,13 +919,16 @@ public function getShippingAddress()
* Order state setter.
* If status is specified, will add order status history with specified comment
* the setData() cannot be overridden because of compatibility issues with resource model
* By default allows to set any state. Can also update status to default or specified value
* Complete and closed states are encapsulated intentionally
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @param bool $shouldProtectState
* @return \Magento\Sales\Model\Order
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setState(
$state,
@@ -934,29 +937,7 @@ public function setState(
$isCustomerNotified = null,
$shouldProtectState = true
) {
return $this->_setState($state, $status, $comment, $isCustomerNotified, $shouldProtectState);
}

/**
* Order state protected setter.
* By default allows to set any state. Can also update status to default or specified value
* Complete and closed states are encapsulated intentionally, see the _checkState()
*
* @param string $state
* @param string|bool $status
* @param string $comment
* @param bool $isCustomerNotified
* @param bool $shouldProtectState
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _setState(
$state,
$status = false,
$comment = '',
$isCustomerNotified = null,
$shouldProtectState = false
) {
// attempt to set the specified state
if ($shouldProtectState) {
if ($this->isStateProtected($state)) {
@@ -965,17 +946,29 @@ protected function _setState(
);
}
}
$this->setData('state', $state);

$transport = new \Magento\Framework\Object(
[
'state' => $state,
'status' => $status,
'comment' => $comment,
'is_customer_notified' => $isCustomerNotified
]
);

$this->_eventManager->dispatch('sales_order_state_change_before', array('order' => $this, 'transport' => $transport));
$status = $transport->getStatus();
$this->setData('state', $transport->getState());

// add status history
if ($status) {
if ($status === true) {
$status = $this->getConfig()->getStateDefaultStatus($state);
$status = $this->getConfig()->getStateDefaultStatus($transport->getState());
}
$this->setStatus($status);
$history = $this->addStatusHistoryComment($comment, false);
$history = $this->addStatusHistoryComment($transport->getComment(), false);
// no sense to set $status again
$history->setIsCustomerNotified($isCustomerNotified);
$history->setIsCustomerNotified($transport->getIsCustomerNotified());
}
return $this;
}
@@ -1166,7 +1159,7 @@ public function registerCancellation($comment = '', $graceful = true)
$this->setTotalCanceled($this->getGrandTotal() - $this->getTotalPaid());
$this->setBaseTotalCanceled($this->getBaseGrandTotal() - $this->getBaseTotalPaid());

$this->_setState($cancelState, true, $comment);
$this->setState($cancelState, true, $comment, null, false);
} elseif (!$graceful) {
throw new \Magento\Framework\Exception\LocalizedException(__('We cannot cancel this order.'));
}