简体   繁体   English

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

[英]Batch change order information as order status for specific Woocommerce order IDs

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. 我有一个订单ID列表(〜400),这些列表当前不在我需要更改的正确订单状态中,我也想更新其付款方式。

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. 到目前为止,我的思考过程是让每个订单ID都有一个数组,依次遍历它们,然后对每个订单执行$order->update_status( 'custom-status' ) 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. 但是我不确定运行此方法的最佳方法,以确保服务器不会超时并且可以批量执行等操作。

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) . 要更新订单ID数组的订单状态,可以使用多种方法(但始终要在数据库备份之前或至少在wp_posts表中进行备份)

Note: Woocommerce order post_status start always by wc- . 注意: Woocommerce订单post_status始终由wc-开始。

1) The best way is to use a lightweight, very efficient and unique SQL query with the WPDB WordPress Class, this way : 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) 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: 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) );
}

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) 我不建议您使用WC_Product方法update_status() ,因为它会很重(并且会向客户发送有关特定订单状态的通知)

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

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