3

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);
        $order->addStatusHistoryComment('');
        $orderState = Order::STATE_PROCESSING;
        $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);// Here I need to add custom status as Cancel initiated
        $order->save();

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

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

3 Answers 3

5

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->setState($state);
        $order->setStatus($status);
        $order->addStatusToHistory($order->getStatus(), $comment);
        $order->save(); 
8
  • It added the status in DB but failed to fetch in Sales->order page for id 3
    – Jaisa
    Commented 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 Commented Nov 27, 2017 at 8:55
  • I need to add status via programatically not in admin... If I said wrong Please correct me
    – Jaisa
    Commented 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. Commented Nov 27, 2017 at 9:00
  • see the updated post , It works fine But failed to fetch in status Dropdown
    – Jaisa
    Commented Nov 27, 2017 at 9:19
3

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
            $status->save();
            // Secondly, assign the status to state
            $status->assignState($statusData['state']);
        } catch (CouldNotSaveException $exception) {
            $this->logger->critical($exception);
        }
4
  • You are welcom, my response helped you or not? Commented Nov 27, 2017 at 10:16
  • Yes. It will work
    – Jaisa
    Commented 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. Commented Feb 9, 2018 at 13:21
  • You must remove all file in var/generation Commented Aug 3, 2018 at 14:48
0

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.

Why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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