2

I want to change the order Status programmatically but when I run the function realTest() as I mentioned below then it gives me an error Call to a member function setState() on array in the below function it mentions line number 5. I also tried function use Magento\Sales\Model\Order; in place of other class as I mentioned in my code but in both scenarios, it gives me the same error also when I tried the array to print_r($orderState) then it returns me null array.

use \Magento\Sales\Api\OrderRepositoryInterface;
public function realTest(){
        try{
            $orderId = 000256076;
            // obtain the order with the order ID
            $order = $this->_orderRepository->get($orderId);
            $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
            $order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
            $this->_orderRepository->save($order);
            return true;
        } catch (\Exception $e){
            echo 'Message: ' .$e->getMessage();
            // add some logging here
            return false;
        }

    }

enter image description here

enter image description here

  • did you check the $order by printing it? – ASQ Sep 20 at 10:42
  • yeah I mention it in my question. I also print_r($order) it reutrns me null array – Asad Khan Sep 20 at 10:44
  • yeah thats the point your code is correct but the order with the respected order id doesn't exist, so how can you apply the status on null, try to test it with the existing order id – ASQ Sep 20 at 10:46
  • this maybe I'm not passing the correct value value of the orderId??? – Asad Khan Sep 20 at 10:49
  • 1
    It seems ridiculous that you are passing the correct order id but still getting the null array – ASQ Sep 20 at 10:56
3

Give a try to below code.

With Increment Id

$incrementId = 000256076;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->loadByIncrementId($incrementId);
$orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
$order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
$order->save();

With Order Id

$orderId = 1271; //check if the order exists with this id or not
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
$orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
$order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
$order->save();
  • Sir I also tried this as well then it gives me an error Uncaught Error: Call to a member function getMethodInstance() on null – Asad Khan Sep 20 at 10:47
  • It seems like the issue is with the $orderId, is your order id exists in admin? – Sumit Sep 20 at 10:48
  • Yes Sir it exists.... – Asad Khan Sep 20 at 10:53
  • For confirmation, Order ID or Order increment number? – Sumit Sep 20 at 10:55
  • Sir check the question I update the question also upload the image from where I get the Order ID – Asad Khan Sep 20 at 11:01
2

Try This :-

/**Magento/Sales/Model/Order $salesOrder**/
$orderId = 12345;
$statusCode = \Magento\Sales\Model\Order::STATE_PROCESSING;

$order = $this->salesOrder->load($orderId);
$order->setState($order->getState())->setStatus($statusCode);
$order->save();
0

Try to implement this check

use \Magento\Sales\Api\OrderRepositoryInterface;
    public function realTest(){
            try{
                $orderId = 256076;
                // obtain the order with the order ID
                $order = $this->_orderRepository->get($orderId);
                if(!empty($order->getData()){
                   $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
                   $order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
                   $this->_orderRepository->save($order);
                   return true;
                }
            } catch (\Exception $e){
                echo 'Message: ' .$e->getMessage();
                // add some logging here
                return false;
            }

        }
0

Your code seems to good but Once try to use following code

use \Magento\Sales\Api\OrderRepositoryInterface;
public function realTest(){
        try{
            $orderId = 1271;
            // obtain the order with the order ID
            $order = $this->_orderRepository->get($orderId);
            $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
            $order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
            $this->_orderRepository->save($order);
            return true;
        } catch (\Exception $e){
            echo 'Message: ' .$e->getMessage();
            // add some logging here
            return false;
        }

    }
  • Still the same issue... – Asad Khan Sep 20 at 10:38
  • Change your order id with real order id which exist in magento. – DJ Dev Sep 20 at 10:53
  • 1
    000256076 it is not the order id. It is order increment id. Click on this order in admin grid then you can get the order id from URL – DJ Dev Sep 20 at 11:07
  • http://localhost/annalou/admin_z3jvbp/sales/order/view/order_id/1271/key/c0c4b310a0090fb81dde1bb8b92fde4aab2987b92d33310fce38da94df15ac84/ this url??? – Asad Khan Sep 20 at 11:09
  • 1
    Order id : 1271 – DJ Dev Sep 20 at 11:09

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.