我将代码放在 Cron 事件中,但无法更新状态



我将代码放入Cron Events中,但无法更新状态
我正在寻找一种方法,将订单状态从pos(我正在使用的自定义状态(自动更改为pos已完成,但在1分钟后可以完成吗?功能代码.pp

add_action('order_status_pos-completed', 'finish_order_status_pos-completed', 10, 1);
function finish_order_status_pos($order_id){
if ( ! $order_id ) {
return;
}
global $product;
$order = wc_get_order( $order_id );
$time_order     = strtotime($order->get_date_created());
$time_current   = time();
$time_interval  = $time_current - $time_order;
//Case refresh page after 3 minutes at order, no changed status
if ($order->data['status'] == 'pos' && $time_interval > 60 ) {
$order->update_status( 'pos-completed' );
}}

我认为order_status_pos-completed钩子不存在,因为它是自定义状态。为什么不使用"wc_order_status"过滤器来重命名现有状态(例如:处理到pos和已完成到pos完成(

add_filter( 'wc_order_statuses', 'wc_rename_order_status' );
function wc_rename_order_status( $order_statuses ) {
foreach ( $order_statuses as $key => $status ) {
if ( 'wc-completed' === $key ) 
$order_statuses['wc-completed'] = _x( 'POS Completed', 'Order status', 'woocommerce' );
}
return $order_statuses;
}

接下来,您可以使用钩子'woococommerce_order_status_processing'&将状态更改为已完成

add_action('woocommerce_order_status_processing', 'finish_order_status_pos-completed', 10, 1);
function finish_order_status_pos($order_id){
if ( ! $order_id ) {
return;
}
global $product;
$order = wc_get_order( $order_id );
$time_order     = strtotime($order->get_date_created());
$time_current   = time();
$time_interval  = $time_current - $time_order;
//Case refresh page after 3 minutes at order, no changed status
if ($order->data['status'] == 'processing' && $time_interval > 60 ) {
$order->update_status( 'completed' );
}}

最新更新