douyou1937 2019-04-01 21:38
浏览 217

批量更改订单信息作为特定Woocommerce订单ID的订单状态

I have a list of order IDs (~400) that are currently not in the correct order status that I need to change, I would also want to update their payment method.

What is the most efficient and best way to approach this?

So far my thought process is to have an array of Order Ids, run through them and then $order->update_status( 'custom-status' ) on each of them. However I am not sure the best way to run this to make sure that the server won't timeout and can do them in batches etc.

  • 写回答

1条回答 默认 最新

  • douda5706 2019-04-01 22:53
    关注

    To update the order status of an array of order Ids, you can use many ways (but always make a database backup before, or at least the wp_posts table).

    Note: Woocommerce order post_status start always by wc-.

    1) The best way is to use a lightweight, very efficient and unique SQL query with the WPDB WordPress Class, this way :

    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) Another way (heavier) is to update the status of an array of order Ids is to use WordPress wp_update_post() function in a foreach loop, this way:

    $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) );
    }
    

    Both codes are tested and works.

    You can embed the code in a function and trigger it from a hook (or even using a shortcode).

    I not recommend you to use the WC_Product method update_status(), as it will be very heavy (and it will send a notification to the customer, for specific order statuses)

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP