在 Woocommerce 中更新状态之前获取最后的旧订单状态 [英] Get last old order status before updated status in Woocommerce

查看:70
本文介绍了在 Woocommerce 中更新状态之前获取最后的旧订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently looking for a way to get the order status before an updated order status.

例如,我的订单状态为 in-progress,我在这里使用此函数以编程方式将订单状态更改为 wc-completed:

So for example my order has the status in-progress and I change the order status programmatically with this function here to wc-completed:

$order->update_status( 'wc-completed' );

当订单状态为 wc-completed 时,我有一个发送电子邮件的触发器.但是我现在需要检查当前状态之前的状态是否为in-progress.如果这是真的,我需要跳过触发器:

When the order status is wc-completed I've a trigger which sends an email. But I need to check now if the status before the current one was in-progress. If this is true, I need to skip the trigger:

$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 );
}

我怎样才能达到这个目标?

How can I reach this?

推荐答案

对于更新前订单状态用$order->update_status('wc-completed');,您需要使用以下内容为每个状态更改事件添加一种状态历史记录:

For that before updating the order status with $order->update_status( 'wc-completed' );, you will need to add a kind of status history on each status change event, using the following:

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 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

USAGE - 那么现在您可以使用以下 IF 语句之一(带有订单 ID):

USAGE - Then now you can use one of the following IF statements (with the Order ID):

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

(使用订单对象):

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

这篇关于在 Woocommerce 中更新状态之前获取最后的旧订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆