I need to add custom status for an order programatically I have below controller file to save order status. I need to add custom status as cancel initiated.Provide me a solution...

Controller file

public function execute()
        $orderId = 3;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
        $orderState = Order::STATE_PROCESSING;
        $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);// Here I need to add custom status as Cancel initiated

        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;

It is failed to fetch or change the status dropdown enter image description here


First create new status Stores > Order Status and set the code for status custom_cancel and use below code

        $orderId = 3;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
        $state = $order->getState();
        $status = 'custom_cancel';
        $comment = '';
        $isNotified = false;
        $order->addStatusToHistory($order->getStatus(), $comment);
| improve this answer | |
  • It added the status in DB but failed to fetch in Sales->order page for id 3 – Jaisa Nov 27 '17 at 8:52
  • @Jayashree add it from admin panel just as i mention and check it is listed in Stores > Order Status – Qaisar Satti Nov 27 '17 at 8:55
  • I need to add status via programatically not in admin... If I said wrong Please correct me – Jaisa Nov 27 '17 at 8:57
  • @Jayashree i know what you want but you have to follow the rule here first add the status in statuses in admin panel then run the code with status code. Then it will be shown in order view if you add directly then in order view it show blank order status.create the status then use it. – Qaisar Satti Nov 27 '17 at 9:00
  • see the updated post , It works fine But failed to fetch in status Dropdown – Jaisa Nov 27 '17 at 9:19

Firstly you must create a custom status and assigned a state like processing. in an upgradeData or InstallData

      $statusData = array('status'=> 'custom', 'state'=> 'processing', 'label' => 'custom state');
      try {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $statusFactory = $objectManager->create('\Magento\Sales\Model\Order\Status\StatusFactory')->load($orderId);

            $status = $statusFactory->create(['data' => $statusData]);
            // Firstly, create the status
            // Secondly, assign the status to state
        } catch (CouldNotSaveException $exception) {
| improve this answer | |
  • You are welcom, my response helped you or not? – Mohamed El Mrabet Nov 27 '17 at 10:16
  • Yes. It will work – Jaisa Nov 27 '17 at 10:19
  • I am getting the following error: Source class "\Magento\Sales\Model\Order\Status\Status" for "Magento\Sales\Model\Order\Status\StatusFactory" generation does not exist. – Mohit Kumar Arora Feb 9 '18 at 13:21
  • You must remove all file in var/generation – Mohamed El Mrabet Aug 3 '18 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.