Skip to content

Commit 4c7ff8f

Browse files
author
David Freiman
committed
Add Event for sales_order_state_change_before dispatched during Order->saveState()
Additionally refactored to remove uneccessary protected function Refactored only call to the protected function to call the public function Fixed up the docblock as well
1 parent e64f88b commit 4c7ff8f

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
@@ -919,13 +919,16 @@ public function getShippingAddress()
919919
* Order state setter.
920920
* If status is specified, will add order status history with specified comment
921921
* the setData() cannot be overridden because of compatibility issues with resource model
922+
* By default allows to set any state. Can also update status to default or specified value
923+
* Complete and closed states are encapsulated intentionally
922924
*
923925
* @param string $state
924926
* @param string|bool $status
925927
* @param string $comment
926928
* @param bool $isCustomerNotified
927929
* @param bool $shouldProtectState
928930
* @return \Magento\Sales\Model\Order
931+
* @throws \Magento\Framework\Exception\LocalizedException
929932
*/
930933
public function setState(
931934
$state,
@@ -934,29 +937,7 @@ public function setState(
934937
$isCustomerNotified = null,
935938
$shouldProtectState = true
936939
) {
937-
return $this->_setState($state, $status, $comment, $isCustomerNotified, $shouldProtectState);
938-
}
939940

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

970963
// add status history
971964
if ($status) {
972965
if ($status === true) {
973-
$status = $this->getConfig()->getStateDefaultStatus($state);
966+
$status = $this->getConfig()->getStateDefaultStatus($transport->getState());
974967
}
975968
$this->setStatus($status);
976-
$history = $this->addStatusHistoryComment($comment, false);
969+
$history = $this->addStatusHistoryComment($transport->getComment(), false);
977970
// no sense to set $status again
978-
$history->setIsCustomerNotified($isCustomerNotified);
971+
$history->setIsCustomerNotified($transport->getIsCustomerNotified());
979972
}
980973
return $this;
981974
}
@@ -1166,7 +1159,7 @@ public function registerCancellation($comment = '', $graceful = true)
11661159
$this->setTotalCanceled($this->getGrandTotal() - $this->getTotalPaid());
11671160
$this->setBaseTotalCanceled($this->getBaseGrandTotal() - $this->getBaseTotalPaid());
11681161

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

0 commit comments

Comments
 (0)