Skip to content

Commit 86ee3db

Browse files
committed
Merge pull request #1199 from dfry22/sales_order_state_change_before
Add Event for sales_order_state_change_before during Order->saveState()
2 parents 78f470b + 26b21de commit 86ee3db

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

app/code/Magento/Sales/Model/Order.php

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -920,13 +920,16 @@ public function getShippingAddress()
920920
* Order state setter.
921921
* If status is specified, will add order status history with specified comment
922922
* the setData() cannot be overridden because of compatibility issues with resource model
923+
* By default allows to set any state. Can also update status to default or specified value
924+
* Complete and closed states are encapsulated intentionally
923925
*
924926
* @param string $state
925927
* @param string|bool $status
926928
* @param string $comment
927929
* @param bool $isCustomerNotified
928930
* @param bool $shouldProtectState
929931
* @return \Magento\Sales\Model\Order
932+
* @throws \Magento\Framework\Exception\LocalizedException
930933
*/
931934
public function setState(
932935
$state,
@@ -935,29 +938,7 @@ public function setState(
935938
$isCustomerNotified = null,
936939
$shouldProtectState = true
937940
) {
938-
return $this->_setState($state, $status, $comment, $isCustomerNotified, $shouldProtectState);
939-
}
940941

941-
/**
942-
* Order state protected setter.
943-
* By default allows to set any state. Can also update status to default or specified value
944-
* Complete and closed states are encapsulated intentionally, see the _checkState()
945-
*
946-
* @param string $state
947-
* @param string|bool $status
948-
* @param string $comment
949-
* @param bool $isCustomerNotified
950-
* @param bool $shouldProtectState
951-
* @return $this
952-
* @throws \Magento\Framework\Exception\LocalizedException
953-
*/
954-
protected function _setState(
955-
$state,
956-
$status = false,
957-
$comment = '',
958-
$isCustomerNotified = null,
959-
$shouldProtectState = false
960-
) {
961942
// attempt to set the specified state
962943
if ($shouldProtectState) {
963944
if ($this->isStateProtected($state)) {
@@ -966,17 +947,29 @@ protected function _setState(
966947
);
967948
}
968949
}
969-
$this->setData('state', $state);
950+
951+
$transport = new \Magento\Framework\Object(
952+
[
953+
'state' => $state,
954+
'status' => $status,
955+
'comment' => $comment,
956+
'is_customer_notified' => $isCustomerNotified
957+
]
958+
);
959+
960+
$this->_eventManager->dispatch('sales_order_state_change_before', array('order' => $this, 'transport' => $transport));
961+
$status = $transport->getStatus();
962+
$this->setData('state', $transport->getState());
970963

971964
// add status history
972965
if ($status) {
973966
if ($status === true) {
974-
$status = $this->getConfig()->getStateDefaultStatus($state);
967+
$status = $this->getConfig()->getStateDefaultStatus($transport->getState());
975968
}
976969
$this->setStatus($status);
977-
$history = $this->addStatusHistoryComment($comment, false);
970+
$history = $this->addStatusHistoryComment($transport->getComment(), false);
978971
// no sense to set $status again
979-
$history->setIsCustomerNotified($isCustomerNotified);
972+
$history->setIsCustomerNotified($transport->getIsCustomerNotified());
980973
}
981974
return $this;
982975
}
@@ -1167,7 +1160,7 @@ public function registerCancellation($comment = '', $graceful = true)
11671160
$this->setTotalCanceled($this->getGrandTotal() - $this->getTotalPaid());
11681161
$this->setBaseTotalCanceled($this->getBaseGrandTotal() - $this->getBaseTotalPaid());
11691162

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

0 commit comments

Comments
 (0)