@@ -919,13 +919,16 @@ public function getShippingAddress()
919
919
* Order state setter.
920
920
* If status is specified, will add order status history with specified comment
921
921
* 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
922
924
*
923
925
* @param string $state
924
926
* @param string|bool $status
925
927
* @param string $comment
926
928
* @param bool $isCustomerNotified
927
929
* @param bool $shouldProtectState
928
930
* @return \Magento\Sales\Model\Order
931
+ * @throws \Magento\Framework\Exception\LocalizedException
929
932
*/
930
933
public function setState (
931
934
$ state ,
@@ -934,29 +937,7 @@ public function setState(
934
937
$ isCustomerNotified = null ,
935
938
$ shouldProtectState = true
936
939
) {
937
- return $ this ->_setState ($ state , $ status , $ comment , $ isCustomerNotified , $ shouldProtectState );
938
- }
939
940
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
- ) {
960
941
// attempt to set the specified state
961
942
if ($ shouldProtectState ) {
962
943
if ($ this ->isStateProtected ($ state )) {
@@ -965,17 +946,29 @@ protected function _setState(
965
946
);
966
947
}
967
948
}
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 ());
969
962
970
963
// add status history
971
964
if ($ status ) {
972
965
if ($ status === true ) {
973
- $ status = $ this ->getConfig ()->getStateDefaultStatus ($ state );
966
+ $ status = $ this ->getConfig ()->getStateDefaultStatus ($ transport -> getState () );
974
967
}
975
968
$ this ->setStatus ($ status );
976
- $ history = $ this ->addStatusHistoryComment ($ comment , false );
969
+ $ history = $ this ->addStatusHistoryComment ($ transport -> getComment () , false );
977
970
// no sense to set $status again
978
- $ history ->setIsCustomerNotified ($ isCustomerNotified );
971
+ $ history ->setIsCustomerNotified ($ transport -> getIsCustomerNotified () );
979
972
}
980
973
return $ this ;
981
974
}
@@ -1166,7 +1159,7 @@ public function registerCancellation($comment = '', $graceful = true)
1166
1159
$ this ->setTotalCanceled ($ this ->getGrandTotal () - $ this ->getTotalPaid ());
1167
1160
$ this ->setBaseTotalCanceled ($ this ->getBaseGrandTotal () - $ this ->getBaseTotalPaid ());
1168
1161
1169
- $ this ->_setState ($ cancelState , true , $ comment );
1162
+ $ this ->setState ($ cancelState , true , $ comment );
1170
1163
} elseif (!$ graceful ) {
1171
1164
throw new \Magento \Framework \Exception \LocalizedException (__ ('We cannot cancel this order. ' ));
1172
1165
}
0 commit comments