/** * 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 \Magento\Sales\Api\Data\OrderInterface $order * @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(\Magento\Sales\Api\Data\OrderInterface $order, $state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = true) { // attempt to set the specified state if ($shouldProtectState) { if ($order->isStateProtected($state)) { throw new \Magento\Framework\Exception\LocalizedException(__('The Order State "%1" must not be set manually.', $state)); } } $transport = new \Magento\Framework\Object(['state' => $state, 'status' => $status, 'comment' => $comment, 'is_customer_notified' => $isCustomerNotified]); $this->eventManager->dispatch('sales_order_state_change_before', ['order' => $this, 'transport' => $transport]); $status = $transport->getStatus(); $order->setData('state', $transport->getState()); // add status history if ($status) { if ($status === true) { $status = $order->getConfig()->getStateDefaultStatus($transport->getState()); } $order->setStatus($status); $history = $order->addStatusHistoryComment($transport->getComment(), false); // no sense to set $status again $history->setIsCustomerNotified($transport->getIsCustomerNotified()); } return $this; }