简体   繁体   English

WooCommerce - 使用 php 代码更改订单状态

[英]WooCommerce - change order status with php code

I am trying to change order status in WooCommerce, but I encountered no luck so far.我正在尝试更改 WooCommerce 中的订单状态,但到目前为止我没有遇到任何运气。 $order instance is created successfully (I know it because echo $order->status; works fine, $order_id is also correct. $order->status = 'pending'; simply doesn't change anything, I do not know why. $order 实例创建成功(我知道这是因为echo $order->status;工作正常,$order_id 也是正确的。 $order->status = 'pending';根本没有改变任何东西,我不知道为什么。

$order = new WC_Order($order_id);
$order->status = 'pending';

Could anyone help me with this?有人可以帮我解决这个问题吗?

Try this code:试试这个代码:

$order = new WC_Order($order_id);
$order->update_status('pending', 'order_note'); // order note is optional, if you want to  add a note to order

Since Woocommerce version 3.0+ to update status you need to do this由于 Woocommerce 3.0+ 版要更新状态,您需要执行此操作

$order = wc_get_order( $order_id );

if($order){
   $order->update_status( 'pending', '', true );
}

Working with woocommerce v4.4 , other answers were not working for me.使用woocommerce v4.4 ,其他答案对我不起作用。 I had to do it this way,我不得不这样做,

$order = wc_get_order($order_id);
$order->set_status('pending');
$order->save();

Note: Woocommerce internally adds wc prefix, you will see it if you view in the database.注意: Woocommerce内部添加了wc前缀,在数据库中查看就会看到。 We do not need to explicitly add it.我们不需要显式添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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