保留并获取Woocommerce 3中的订单状态历史及其时间戳
创始人
2024-11-24 09:02:26
0

您可以通过以下代码示例来保留并获取Woocommerce 3中的订单状态历史及其时间戳:

// 添加订单状态历史记录
function add_order_status_history($order_id, $status) {
    $order = wc_get_order($order_id);
    $order->add_order_note(
        sprintf(
            __('Order status changed to %s', 'woocommerce'),
            wc_get_order_status_name($status)
        )
    );
}

// 捕获订单状态转换以记录历史记录
function capture_order_status_transition($order_id, $old_status, $new_status) {
    if ($old_status != $new_status) {
        add_order_status_history($order_id, $new_status);
    }
}
add_action('woocommerce_order_status_transition', 'capture_order_status_transition', 10, 3);

// 获取订单状态历史记录及其时间戳
function get_order_status_history($order_id) {
    $order = wc_get_order($order_id);
    $order_notes = $order->get_customer_order_notes();

    $history = array();
    foreach ($order_notes as $note) {
        $history[] = array(
            'status' => $note->comment_content,
            'timestamp' => $note->comment_date
        );
    }

    return $history;
}

要使用上述代码,请将其添加到您的主题的functions.php文件中或将其添加到自己的插件中。然后,您可以使用get_order_status_history()函数来获取订单状态历史记录及其时间戳。

使用示例:

$order_id = 123; // 替换为您要获取历史记录的订单ID
$order_history = get_order_status_history($order_id);
foreach ($order_history as $history) {
    echo 'Status: ' . $history['status'] . '
'; echo 'Timestamp: ' . $history['timestamp'] . '

'; }

这将遍历订单的历史记录,并将订单状态和时间戳打印出来。您可以根据需要自行调整输出的格式和样式。

相关内容

热门资讯

6个德州扑克!wpk ai检测... 6个德州扑克!wpk ai检测(数据统计)原来真的有挂(有挂透明挂)-哔哩哔哩;亲,其实确实真的有挂...
第2插件!越乡游义乌有挂吗(辅... 第2插件!越乡游义乌有挂吗(辅助挂)太坑了原来真的有挂(有挂规律)-哔哩哔哩1、实时越乡游义乌有挂吗...
1外挂(wpk开挂)外挂透明挂... 1外挂(wpk开挂)外挂透明挂辅助脚本(辅助挂)AA德州教程!(有挂教程)-哔哩哔哩是一款可以让一直...
第6个线上!德扑软件决策(ai... 第6个线上!德扑软件决策(ai软件)软件透明挂(有挂解惑)-哔哩哔哩相信很多朋友都在电脑上玩过吧,但...
第4模拟器(德扑之星房间设置)... 1、第4模拟器(德扑之星房间设置)外挂透明挂辅助软件(透视)科技教程(真实有挂)-哔哩哔哩。2、德扑...
四个辅助!wpk透视(机制)原... 四个辅助!wpk透视(机制)原来真的有挂(有挂攻略)-哔哩哔哩1、每一步都需要思考,不同水平的挑战会...
第1神器!白金岛字牌集外挂(透... 第1神器!白金岛字牌集外挂(透视)太坑了其实真的有挂(今日头条)-哔哩哔哩您好,白金岛字牌集外挂,确...
第一个总结!德扑ai代打(牌型... 第一个总结!德扑ai代打(牌型胜率)软件透明挂(有挂分享)-哔哩哔哩1、每一步都需要思考,不同水平的...
第五俱乐部(wepoke真的)... 第五俱乐部(wepoke真的)外挂透明挂辅助器(辅助挂)可靠技巧(有挂细节)-哔哩哔哩;实战中需综合...
五个检测!wpk怎么下载ios... 五个检测!wpk怎么下载ios(外挂被实锤)原来真的有挂(有挂实锤)-哔哩哔哩;支持2-10人实时对...