3
<?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;    public function __construct(
       Context $context,
       \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
   ) {
       parent::__construct($context);
       $this->resultJsonFactory = $resultJsonFactory;
   }    /**
    * Dispatch request
    *
    * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
    * @throws \Magento\Framework\Exception\NotFoundException
    */
   public function execute()
   {
                        $data = json_decode(file_get_contents('php://input'), true);
                        // $gff = $data['events'];
                         // print_r($gff);
                         $orderId = $data['id'];
                         $events = $data['events'];
                         $array = array();
                         foreach($events as $key => $value){
                         //print_r($value['id']);
                             $array[$key] = $value['code'];                             
                        //      if($value['code'] == "246")
                        // {
                        //     // print_r($value);
                        //         $orderId = $orderId;
                        //         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                        //         $order = $objectManager->create('Magento/Sales/Model/Order')
                        //                    ->load('1372072');
                        //         $order->setState("pending")->setStatus("pending");
                        //         $order->save();
                        // }
                         }
                         print_r($array);
   }
}
2

As you want to update order then you should load

Order factory class/Repository class at Your controller and then using this class , can change order status;

Here i am going to use Repository Magento\Sales\Model\OrderRepository So,you should inject this class on __construct()

<?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()
   {
       $_order = $this->_orderRepository->getById($orderId); // it order is not order increment id
       $_order->setStatus('pending')->setState('pending');
       $_order->save();
    }
}
  • thanks for reply i ma getting error this when run the controller Exception #0 (RuntimeException): Class Magento\Sales\Model does not exist – Rahul Katoch Mar 23 '17 at 8:28
  • @RahulKatoch I'm also facing the same issue did you get the solution? – Asad Khan Sep 19 at 12:42

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.