【问题标题】:WooCommerce - change order status with php codeWooCommerce - 使用 php 代码更改订单状态
【发布时间】:2014-05-21 01:12:14
【问题描述】:

我正在尝试在 WooCommerce 中更改订单状态,但到目前为止我没有遇到任何运气。 $order 实例创建成功(我知道是因为echo $order->status; 工作正常,$order_id 也正确。$order->status = 'pending'; 根本没有改变任何东西,我不知道为什么。

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

谁能帮我解决这个问题?

【问题讨论】:

    标签: php class object field woocommerce


    【解决方案1】:

    试试这个代码:

    $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
    

    【讨论】:

    • 我明天会检查这个代码,如果它有效,则将此答案标记为正确:)。
    • 致命错误:在第 54 行的 /public_html/wp-content/plugins/woocommerce/includes/wc-order-functions.php 中的非对象上调用成员函数 get_order()跨度>
    • @itskawsar 这意味着找不到订单,可能是 $order_id 错误
    【解决方案2】:

    由于 Woocommerce 版本 3.0+ 要更新状态,您需要这样做

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

    【讨论】:

      【解决方案3】:

      使用woocommerce v4.4,其他答案对我不起作用。我不得不这样做,

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

      注意: Woocommerce 内部添加了wc 前缀,在数据库中查看会看到。我们不需要显式添加它。

      【讨论】:

      • 它修改的文件是什么?
      • @Dario 这里的代码,将订单状态保存在数据库中。它不会修改文件。
      猜你喜欢
      • 2017-05-24
      • 2016-12-12
      • 2018-01-01
      • 2019-02-01
      • 2022-09-30
      • 2018-08-30
      • 2019-08-27
      • 2022-10-05
      • 2021-04-08
      相关资源
      最近更新 更多