Php 获取Woocommerce中更新状态之前的上一个旧订单状态

Php 获取Woocommerce中更新状态之前的上一个旧订单状态,php,wordpress,woocommerce,metadata,orders,Php,Wordpress,Woocommerce,Metadata,Orders,我目前正在寻找一种在更新订单状态之前获取订单状态的方法 例如,我的订单状态为“进行中”,我使用此函数以编程方式将订单状态更改为“wc completed”: 当订单状态为wc completed时,我有一个发送电子邮件的触发器。但是我现在需要检查一下,当前进程之前的状态是否正在进行中。如果这是真的,我需要跳过触发器: $latest_status = get_order_status_before_current_one($order_id); if ($latest_status !== 'i

我目前正在寻找一种在更新订单状态之前获取订单状态的方法

例如,我的订单状态为“进行中”,我使用此函数以编程方式将订单状态更改为“wc completed”:

当订单状态为wc completed时,我有一个发送电子邮件的触发器。但是我现在需要检查一下,当前进程之前的状态是否正在进行中。如果这是真的,我需要跳过触发器:

$latest_status = get_order_status_before_current_one($order_id);
if ($latest_status !== 'in-progress') {
    // Triggers for this email.
    add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ), 1, 2 );
}
如何达到此目的?

在使用$order->update_status“wc completed”更新订单状态之前;,您需要使用以下方法在每个状态更改事件上添加一种状态历史记录:

add_action( 'woocommerce_order_status_changed', 'grab_order_old_status', 10, 4 );
function grab_order_old_status( $order_id, $status_from, $status_to, $order ) {
    if ( $order->get_meta('_old_status') ) {
        // Grab order status before it's updated
        update_post_meta( $order_id, '_old_status', $status_from );
    } else {
        // Starting status in Woocommerce (empty history)
        update_post_meta( $order_id, '_old_status', 'pending' );
    }
}
代码进入活动子主题或活动主题的function.php文件。测试和工作

用法-现在您可以使用以下带有订单ID的IF语句之一:

或使用Order对象:

在使用$order->update_status“wc completed”更新订单状态之前;,您需要使用以下方法在每个状态更改事件上添加一种状态历史记录:

add_action( 'woocommerce_order_status_changed', 'grab_order_old_status', 10, 4 );
function grab_order_old_status( $order_id, $status_from, $status_to, $order ) {
    if ( $order->get_meta('_old_status') ) {
        // Grab order status before it's updated
        update_post_meta( $order_id, '_old_status', $status_from );
    } else {
        // Starting status in Woocommerce (empty history)
        update_post_meta( $order_id, '_old_status', 'pending' );
    }
}
代码进入活动子主题或活动主题的function.php文件。测试和工作

用法-现在您可以使用以下带有订单ID的IF语句之一:

或使用Order对象:

if( get_post_meta( $order_id, '_old_status', true ) !== 'in-progress' ) { 
    // Your code
}
if( $order->get_meta('_old_status') !== 'in-progress' ) { 
    // Your code
}