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

3 Answers 3


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);
  • It added the status in DB but failed to fetch in Sales->order page for id 3
    – Jaisa
    Nov 27, 2017 at 8:52
  • @Jayashree add it from admin panel just as i mention and check it is listed in Stores > Order Status Nov 27, 2017 at 8:55
  • I need to add status via programatically not in admin... If I said wrong Please correct me
    – Jaisa
    Nov 27, 2017 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. Nov 27, 2017 at 9:00
  • see the updated post , It works fine But failed to fetch in status Dropdown
    – Jaisa
    Nov 27, 2017 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) {
  • You are welcom, my response helped you or not? Nov 27, 2017 at 10:16
  • Yes. It will work
    – Jaisa
    Nov 27, 2017 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. Feb 9, 2018 at 13:21
  • You must remove all file in var/generation Aug 3, 2018 at 14:48

i created:

$order = $objectManager->create('\Magento\Sales\Model\Order')->load($order_number);
$order->setState(\Magento\Sales\Model\Order::STATE_CANCELED, true);

If i check the status programmaticaly with

$order = $objectManager->create('\Magento\Sales\Model\Order')->load($order_number);
return $order->getState();

the status is canceled but if i see to frontend it not changed.


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.