【问题标题】:Batch change order information as order status for specific Woocommerce order IDs批量更改订单信息作为特定 Woocommerce 订单 ID 的订单状态
【发布时间】:2019-08-23 03:21:47
【问题描述】:

我有一个订单 ID (~400) 的列表,目前我需要更改的订单状态不正确,我还想更新他们的付款方式。

解决此问题的最有效和最佳方法是什么?

到目前为止,我的想法是创建一个订单 ID 数组,遍历它们,然后在每个 ID 上运行$order->update_status( 'custom-status' )。但是我不确定运行它以确保服务器不会超时并且可以批量执行它们等的最佳方法。

【问题讨论】:

    标签: php sql wordpress woocommerce orders


    【解决方案1】:

    要更新订单 ID 数组的订单状态,您可以使用多种方式(但始终在之前备份数据库,或者至少备份 wp_posts 表)

    注意: Woocommerce 订单 post_status 始终以 wc- 开头。

    1) 最好的方法是使用WPDB WordPress 类的轻量级、非常高效且唯一的 SQL 查询,这样:

    global $wpdb;
    
    $new_status = 'wc-custom-status';
    $product_ids = array(37, 53, 57, 63, 80); // The array of product Ids
    $product_ids = implode(',', $product_ids);
    
    $wpdb->query( "
        UPDATE {$wpdb->prefix}posts
        SET post_status = '$replacement_user_id'
        WHERE ID IN ($product_ids)
        AND post_type = 'shop_order'
    " );
    

    2)另一种方法 (较重)是更新订单ID数组的状态是在foreach循环中使用WordPress wp_update_post()函数,这种方式:

    $new_status = 'wc-custom-status';
    $product_ids = array(37, 53, 57, 63, 80); // The array of product Ids
    $product_ids = implode(',', $product_ids);
    
    foreach ( $orders_ids as $order_id ) {
        wp_update_post( array('ID' => $order_id, 'post_status' => $new_status) );
    }
    

    这两个代码都经过测试并且可以正常工作。

    您可以将代码嵌入到函数中并通过挂钩(甚至使用简码)触发它。

    我不建议你使用WC_Product方法update_status(),因为它会很重(它会向客户发送通知,以获取特定的订单状态)

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2021-04-08
      • 2018-08-30
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      相关资源
      最近更新 更多