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