【问题标题】:Bulk update order status in WooCommerce every XXX hours [closed]每 XXX 小时在 WooCommerce 中批量更新订单状态 [关闭]
【发布时间】:2020-05-22 20:47:13
【问题描述】:
  • 我正在尝试以编程方式每 6 小时更新一次所有订单状态。
  • “完成”后有 3 种自定义状态,每 6 小时按顺序更新一次。
  • 我创建了一个 cron,每 6 小时运行一次,调用函数“auto_update_orders”。

这样做更好还是直接 SQL 查询更好?如果通过 SQL 查询完成,是否还会发送自定义状态的自动电子邮件?

function auto_update_orders() {

$args = array(
    'post_type'         => 'shop_order',
    'posts_per_page'    => -1,
    'post_status'       => 'complete',
);

$orderList = get_posts($args);

foreach ($orderList as $orderPost) {

    $order = new WC_Order($orderPost->ID);

    if ( $order->has_status( 'completed' ) ) {
        $order->update_status( 'inprogress2' );
    }
    elseif ( $order->has_status( 'inprogress2' ) ) {
        $order->update_status( 'inprogress3' );
    }
    elseif ( $order->has_status( 'inprogress3' ) ) {
        $order->update_status( 'ready' );
    }
    else {
        return;
    }

}

【问题讨论】:

  • 使用WC_Order 对象是最好的方法,而不是直接运行SQL 查询。 WC_Order update_status 调用许多其他函数
  • 谢谢...上面的代码正确吗?
  • 是的,没错。
  • 添加并执行但没有。
  • 使用wc_get_orders 代替get_posts

标签: php mysql wordpress woocommerce


【解决方案1】:

我会使用更多内置的 WooCommerce 功能。有一天,Woo 会将订单移动到自定义表(真的认为它已经发生了)并且wc_get_orders() 将保持兼容。

function auto_update_orders() {

    $args = array(
        'limit'  => -1,
        'status' => array( 'complete', 'inprogress2', 'inprogress3 )'
    );

    $orderList = wc_get_orders($args);

    foreach ($orderList as $order) {

        if ( $order->has_status( 'completed' ) ) {
            $order->update_status( 'inprogress2' );
        }
        elseif ( $order->has_status( 'inprogress2' ) ) {
            $order->update_status( 'inprogress3' );
        }
        elseif ( $order->has_status( 'inprogress3' ) ) {
            $order->update_status( 'ready' );
        }

    }

}

我整个星期都在玩动作调度器。根据您可以预期wc_get_orders() 返回的订单数量,将其分成较小的批次可能是一个不错的选择。

【讨论】:

  • 我添加了这个并通过 Advanced Cron Manager 执行,完成的订单没有任何变化。
  • 没关系...'status' => array('complete', 'inprogress2', 'inprogress3)' to 'status' => array('completed', 'inprogress2', 'inprogress3' ) 谢谢!
  • 注意:save()方法已经在update_status()方法中使用了...
猜你喜欢
  • 2018-01-01
  • 1970-01-01
  • 2017-07-16
  • 2019-04-24
  • 2017-05-24
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多