Magento 2: Call to a member function setState() on array
-
14-03-2021 - |
Question
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;
}
}
Solution
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();
OTHER TIPS
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();
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;
}
}
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;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange