是否有任何方法可以在经过这么多时间之后将自定义订单状态自动将自定义订单状态更改为其他自定义订单状态?
基本上,我希望将所有更改为订单状态的订单自动更改为30天后的"退款预期"。
我意识到这不是普通的WooCommerce订单状态,而是我创建了自定义。因此,如果我不太具体的话,可能会更容易理解我想要的...
我只想能够将已更改为特定状态的订单自动在这么多天后自动更改为不同的状态。
我遍布互联网,找不到任何可以帮助我实现这一目标的东西。任何帮助将不胜感激。
function order_status_changer() {
global $wpdb;
$result = $wpdb->get_results("
SELECT *
FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status = 'wc-completed'
");
$args = array(
'date_query' => array(
array(
'column' => 'post_modified_gmt', // post modifed one month ago
'after' => '1 month ago',
),
),
'meta_query' => array(
array(
'key' => '_refund_expired', // skip posts that is already updated in last run
'compare' => 'NOT EXISTS'
)
),
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => 'refund-submitted', // slug of your status to modfiy
);
$query = new WP_Query($args);
$posts = $query->posts;
foreach ($posts as $post) {
$ord = new WC_Order($post->ID);
$ord->update_status('refund-expired'); // Slug to which these order to be updated
update_post_meta($post->ID, '_refund_expired', 1); // update posts meta to note the status change for skipping on next read
}
}
order_status_changer();
将此代码添加到主题的functions.php
将此代码添加到主题functions.php
您可以更改天(如果($ int> 1)):在此示例中:1天
(wp_)是table_prefix
add_action('init', 'wp_orders');
function wp_orders()
{
global $wpdb;
$my_query = "SELECT * FROM wp_wc_order_stats where STATUS='wc-processing'";
$val123 = $wpdb->get_row($my_query, OBJECT);
$result2 = $wpdb->get_results($my_query);
foreach ($result2 as $results2) {
$date1 = $results2->date_created_gmt;
$order_id = $results2->order_id;
$date2=date("Y-m-d h:i:s");
$dteStart = new DateTime($date1);
$dteEnd = new DateTime($date2);
$dteDiff = $dteStart->diff($dteEnd);
$Diff = $dteDiff->format("%d");
$int = (int)$Diff;
if($int>1)
{
$order = new WC_Order($order_id);
if (!empty($order)) {
$order->update_status( 'completed' );
}
}
}
}