0

I'm trying to change the specific order Order Status. for that I tried.

<?php
namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
class Index extends Action
{
/**
    * @var \Magento\Framework\Controller\Result\JsonFactory
    */
   protected $resultJsonFactory;  
   protected $_orderRepository ;
   public function __construct(
       Context $context,
       Magento\Sales\Model $orderRepository,
       \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
   ) {
       parent::__construct($context);
       $this->resultJsonFactory = $resultJsonFactory;
       $this->_orderRepository = $orderRepository;
   }    /**
    * Dispatch request
    *
    * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
    * @throws \Magento\Framework\Exception\NotFoundException
    */
   public function execute()
   {
       $orderid = 256076;
       $_order = $this->_orderRepository->getById($orderId); // it order is not order increment id
       $_order->setStatus('pending')->setState('pending');
       $_order->save();
    }
}

but this give me an error which is Exception #0 (RuntimeException): Class Magento\Sales\Model does not exist How can I change The Order Status from Pending to Processing and every other order status?

0
use Magento\Sales\Model\Order;
$orderId = 1;
$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();

Please check with this code..

  • Direct use of Object Manager not recommended – Oscprofessionals Sep 19 at 10:50
0

Change like this

use Magento\Sales\Model\Order;

public function __construct(
       Context $context,
       \Magento\Sales\Model\Order $orderRepository,
       \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
   ) {
       parent::__construct($context);
       $this->resultJsonFactory = $resultJsonFactory;
       $this->_orderRepository = $orderRepository;
   } 


$order = $this->_orderRepository;
$order->load($orderId);
$orderState = Order::STATE_PROCESSING;
$order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
$order->save();
  • this error occurs.. – Asad Khan Sep 19 at 10:53
  • Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Sales\Model\Order\Interceptor::create – Asad Khan Sep 19 at 10:58
  • need to di compile php bin/magento setup:di:compile – Arunprabakaran M Sep 19 at 11:00
  • I fixed the issue. remove create(). code tested. try now – Arunprabakaran M Sep 19 at 11:25
  • Asad Khan try this – Arunprabakaran M Sep 19 at 11:29
0

Please try this code of snippet, It will help you

<?php
namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Sales\Model\Order;

class Index extends Action
{
/**
    * @var \Magento\Framework\Controller\Result\JsonFactory
    */
   protected $_salesOrder;
   protected $resultJsonFactory;  
   protected $_orderRepository;
   public function __construct(
       Context $context,
       \Magento\Sales\Model $orderRepository,
       \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
       \Magento\Sales\Model\Order $salesOrder
   ) {
       parent::__construct($context);
       $this->resultJsonFactory = $resultJsonFactory;
       $this->_orderRepository = $orderRepository;
       $this->_salesOrder = $salesOrder;
   }    /**
    * Dispatch request
    *
    * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
    * @throws \Magento\Framework\Exception\NotFoundException
    */
   public function execute()
   {
       $orderid = 256076;
       $order = $this_>_salesOrder->create();
       $order->load($orderid);
       $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
       $order->setState($orderState)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
       $order->save();
    }
}
  • thanks for reply i ma getting error this when run the controller Exception #0 (RuntimeException): Class Magento\Sales\Model does not exist – Asad Khan Sep 19 at 11:05
  • Use Magento\Sales\Model\Order instead of Magento\Sales\Model. – Oscprofessionals Sep 19 at 11:08
  • Please also run whole command: php bin/magento s:up && php bin/magento s:d:c && php bin/magento s:s:d -f && php bin/magento c:c && php bin/magento c:f – Oscprofessionals Sep 19 at 11:13
  • Exception #0 (LogicException): AsadTest\Asad\Block\Test does not implement BlockInterface – Asad Khan Sep 19 at 11:25
  • now this error occurs. – Asad Khan Sep 19 at 11:25

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.