我需要在7天后将订单状态自动更改为(取消),如果它没有被处理。
我需要将状态从:新建请求更改为已取消。或我需要将状态从:正在处理更改为已取消。
谢谢Adnan
我是在这段代码的帮助下完成的:
function autocancel_wc_orders(){
$query = ( array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'status' => array( 'wc-pending', 'wc-ywraq-new', 'wc-ywraq-
pending')
) );
$orders = wc_get_orders( $query );
foreach( $orders as $order ){
$date = new DateTime( $order->get_date_created() );
$today = new DateTime();
$interval = $date->diff($today);
$datediff = $interval->format('%a');
if( $datediff > 2 ){
$order->update_status('cancelled', 'Cancelled for missing
payment');
}
}
}
add_action( 'admin_init', 'autocancel_wc_orders' );
我在网上找到了这个答案:https://samuelsilva.pt/cancel-woocommerce-not-paid-orders-automatically/
帮助文档- https://developer.wordpress.org/reference/classes/wp_query/#date-parametershttps://woocommerce.wp-a2z.org/oik_api/wc_get_order_statuses/
//My server cronjob is targeting wp-cron.php
function ss_cancel_failed_orders() {
$held_duration = 15; //minutes how often my cron job to run per day/hour
$data_store = WC_Data_Store::load('order');
//Change post_status for desired order statuses
//Change date_query before value to desired time compare with modified post date
$unpaid_orders = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_order', 'post_status' => array('wc-failed','wc-on-hold'), 'date_query' => array(array('before' => '15 minutes ago', 'column'=>'post_modified' ))));
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
$order->update_status( 'cancelled', __( 'Order has expired.', 'woocommerce' ) ); // Status here is without wc- prefix
}
}
wp_clear_scheduled_hook( 'ssa_cancel_failed_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'ssa_cancel_failed_orders' ); // you can remove wp cron and work with server cron only
}
add_action('ssa_cancel_failed_orders', 'ss_cancel_failed_orders');