更新 4
注意: 在 WooCommerce 中,there is already a function 挂钩 woocommerce_cancel_unpaid_orders 动作挂钩,在 7 天后取消未付款订单。
我没有找到woocommerce_cancel_unpaid_submitted动作钩子,所以不知道它是否存在,是否被触发。
现在您的代码中有一些错误,您可以更好地使用wc_get_orders(),它可以直接为您提供正确的WC_Order 对象数组...
这里有一些不同的方法来制作它(最后一个未经测试):
1) 最后一个解决方案经过测试并且有效当商店经理或管理员用户角色浏览管理员订单列表时(每天只执行一次):
add_action( 'restrict_manage_posts', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
global $pagenow, $post_type;
// Enable the process to be executed daily when browsing Admin order list
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5; // <=== 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' => 'on-hold',
'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 orders
foreach ( $unpaid_orders as $unpaid_order ) {
$unpaid_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;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。
2) 这第三种解决方案 经过测试并且有效:当任何订单更改为“处理中”或“已完成”状态时触发该功能(仅执行一天一次):
// Triggered on orders status change to "processing" or "completed"
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 = 5; // <=== 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' => 'on-hold',
'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 $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;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。
3) 所以你可以试试woocommerce_cancel_unpaid_submitted action hook:
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$days_delay = 5; // <=== 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 here)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'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 orders
foreach ( $unpaid_orders as $order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。
功能代码应该更好地工作。对于钩子我真的不知道。
4) 您也可以尝试使用woocommerce_cancel_unpaid_orders 动作挂钩。