4

I have created custom Api to update order status by using Order Id. I have used the below code in webapi.xml file.

<route url="/V1/orderstatus/orderUpdate" method="POST">
    <service class="[Vendor]\[Module]\Api\CustomOrderInterface" method="orderUpdate"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

Below code used in [Vendor]/[Module]/Api/CustomOrderInterface.php

<?php
  namespace [Vendor]\[Module]\Api; 
      interface CustomOrderInterface
       {
         /**
          * Returns greeting message to user
          *
          * @api
          * @param string $name Users name.
          * @return string Greeting message with users name.
         */
        public function name($name);

          /**
           * POST for attribute api
           * @param mixed $param
           * @return array
          */

           public function orderUpdate($params);


       }

Here is my Model file

      <?php
       namespace [Vendor]\[Module]\Model;
        use [Vendor]\[Module]\Api\CustomOrderInterface;
       class Orderstatus implements CustomOrderInterface
         {  
          protected $_storeManager;
          public function __construct(
          \Magento\Framework\ObjectManagerInterface $objectManager,
          \Magento\Store\Model\StoreManagerInterface $storeManager
          ) {
            $this->_objectManager = $objectManager;
            $this->_storeManager = $storeManager;
           }

       /**
        * Returns greeting message to user
       *
       * @api
       * @param string $name Users name.
       * @return string Greeting message with users name.
       */
public function name($name) {
    return "Hello, " . $name;
}

public function orderUpdate($params) {
    //return 'Response: ' . json_encode($params);   
    $resultArr = array();
    $paramArr = array();
    $result =  json_encode($params);
    $resultSet = json_decode($result,true);
    //print_r($resultSet);die;
    $orderData = $resultSet['Entity_Type'];
    $orderId = $resultSet['Entity_Attributes']['Order_ID'];
    $status = $resultSet['Entity_Attributes']['Status'];

    try{
        $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);           
        $order->save();

        $res = "Success";
        $paramArr['Magento_ID'] = '';
        $resultArr['result'] = $res;
        $resultArr['message'] = 'Order status changed Successfully';
        $resultArr['parameters'] = $paramArr;
    }catch(Exception $e){
            $resultArr['result'] = "Failed";
            $resultArr['message'] = $e->getMessage();
            $paramArr['Magento_ID'] = '';
            $resultArr['parameters'] = $paramArr;
    }    

     echo json_encode($resultArr);

 }

}

How to change the order status by Order Id using custom api?. I am trying to change order status to custom status i created status code is 'send_print'.Please anyone suggest.

Your Answer

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

Browse other questions tagged or ask your own question.