cancel
Showing results for 
Search instead for 
Did you mean: 

Change order status via PHP with Magento2

Change order status via PHP with Magento2

Using Magento ver. 2.0.7
I am trying to use this code to change order to the status of "Processing", but i get an error, i think i am missing some prerequisite to call, but i can't figure it out.

> [25-Oct-2016 13:11:30 America/Detroit] PHP Fatal error: Class 'Magento\Framework\App\ObjectManager' not found in /chroot/home/testplat/test.platinait.ca/html/pub/orderstatus.php on line 4

 

<?php

$orderId = 000000193;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')
->load($orderId); 

$order->setState("processing")->setStatus("processing");

$order->save();
4 REPLIES 4

Re: Change order status via PHP with Magento2

you must define the class "ObjectManager"

Re: Change order status via PHP with Magento2

Hello,

I'm facing the same issue, did you manage to get this solved ?

Re: Change order status via PHP with Magento2

 

<?php
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
error_reporting(E_ALL);
require dirname(__DIR__) . '/app/bootstrap.php';
require dirname(__DIR__) .'/pub//excel_reader.php';
error_reporting(0);
$opt = $_GET;
try{
if (empty($opt['group'])) {
$opt['group'] = 'default';
}
foreach ($opt as $key => $value) {
$opt[$key] = escapeshellarg($value);
}
$opt['standaloneProcessStarted'] = '0';
$params = $_SERVER;
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
$params[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
error_reporting(0);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Cron $app */
$app = $bootstrap->createApplication('Magento\Framework\App\StaticResource', ['parameters' => $opt]);
$bootstrap->run($app);
error_reporting(0);
} catch (\Exception $e) {
echo $e;
exit(1);
}
print '<pre>';
 
 
$orderState = getStatusCode(removeIncompleteString($cell[2]));
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->create('\Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
try{
$order = $objectManager->create('\Magento\Sales\Model\Order')->loadByIncrementId($orderIncId);
if(!$order->getId()){
print_r("No Order found with this order id - ".$orderIncId."<br>");
continue;
}
$order->setState($orderState)->setStatus($orderState);
$order->save();
 
function getStatusCode($status){
$statusCode = '';
switch ($status) {
case "Suspected Fraud ":
$statusCode = 'fraud';
break;
case "Payment Review":
$statusCode = 'payment_review';
break;
case "Pending":
$statusCode = 'pending';
break;
case "On Hold":
$statusCode = 'holded';
break;
case "Complete":
$statusCode = 'complete';
break;
case "Closed":
$statusCode = 'closed';
break;
default:
$statusCode = strtolower(str_replace(" ","_",trim($status)));
}
return $statusCode;
}

Re: Change order status via PHP with Magento2

Hey,

check below code:-

<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$orderId = 000000193;
$order = $obj->create('\Magento\Sales\Model\Order')->load($orderId); 
$order->setState("processing")->setStatus("processing");
$order->save();

If my answer is right then accepted as the solution.

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer