简体   繁体   中英

Magento 2: change order status programmatically

I need to set in "canceled" all orders stucks in "pending" status. The code that I used return this exception error:

[2022-12-03 08:00:53] main.CRITICAL: Please provide payment for the order.

Here the code:

use Magento\Sales\Model\Order;

protected $order;

public function __construct(Order $order)
{
    $this->order = $order;
}

public function orderStatusChange()
{
    $orderId = 9999;
    $order = $this->order->load($orderId);
    $order->setStatus("canceled");
    $order->save();
 }
Please create a new file on the magento2 root and add below code:

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';    
$params = $_SERVER;    
$bootstrap = Bootstrap::create(BP, $params);    
$obj = $bootstrap->getObjectManager();    
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$orderId = '12345';
$order = $obj->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
$order->setStatus("canceled");
$order->setState("canceled");
$order->save();
echo "Order updated";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM