【发布时间】:2019-10-31 16:30:28
【问题描述】:
@LoicTheAztec 回答 this Question 的延续是一个很好的解决方案。但我需要对其进行一些更新。我想将时间延迟更新为 30 分钟或 1 小时,而不是 5 天。
它还应该适用于订单状态挂起/暂停。
问题是当我每天尝试使用此代码执行“待付款”时,它会执行最后一个自动取消的订单。
所以代码应该避免最后一个订单或者应该检查下订单的时间。
add_action( 'woocommerce_order_status_changed', 'daily_cancel_unpaid_orders', 10, 4 );
function daily_cancel_unpaid_orders( $order_id, $old_status, $new_status, $order ) {
// Enable the process to be executed daily
if( in_array( $new_status, array('processing', 'completed') )
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 1; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'pending',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through WC_Order Objects
foreach ( $unpaid_orders as $unpaid_order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
// Schedule the process to the next day (executed once restriction)
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
【问题讨论】:
-
这很奇怪。最后一个订单如何通过
'date_created' => '<' . ( $today - ($days_delay * $one_day) )约束?此外,我在您的代码中没有看到任何将时间间隔减少到 30m 或 1h 的迹象……我说得对吗? -
是的,我想知道如何设置时间现在是一天
标签: php wordpress woocommerce