You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于程序添加的订单备注自动更新WooCommerce订单状态

WooCommerce自动订单备注触发状态更新解决方案

问题根源

你的代码只对管理员手动备注生效,主要有两个问题:

  • 调用wc_get_order_notes时限定了type => 'internal',程序自动添加的备注大多不属于这个类型,直接被过滤掉了;
  • 函数没有利用woocommerce_order_note_added钩子的参数,反而全局遍历所有processing订单,既低效又没必要。

修复后的代码

// 挂钩订单备注添加事件,接收备注ID和订单对象两个参数
add_action( 'woocommerce_order_note_added', 'update_order_status_based_on_delhivery_strings', 10, 2 );

/**
 * 根据Delhivery发货备注更新订单状态为已完成
 * @param int $note_id 新增备注的ID
 * @param WC_Order $order 对应的订单对象
 */
function update_order_status_based_on_delhivery_strings( $note_id, $order ) {
    // 只处理processing状态的订单
    if ( $order->get_status() !== 'processing' ) {
        return;
    }

    // 获取刚添加的备注
    $note = wc_get_order_note( $note_id );
    if ( ! $note ) {
        return;
    }

    $note_content = $note->content;

    // 检查备注是否包含指定的Delhivery标识
    $has_delhivery_info = (
        stripos( $note_content, 'SHIPMENT NUMBER :' ) !== false &&
        stripos( $note_content, 'SHIPPER : Delhivery' ) !== false &&
        stripos( $note_content, 'TRACKING : https://www.delhivery.com/track/package/' ) !== false
    );

    if ( $has_delhivery_info ) {
        // 记录调试日志
        error_log( '自动更新订单状态为已完成,订单ID: ' . $order->get_id() );

        // 更新状态并添加备注说明
        $order->update_status( 'completed', '检测到Delhivery发货信息,自动标记为已完成' );
    }
}

核心修改点

  • 取消备注类型限制:不再限定只查internal类型的备注,确保程序自动添加的备注能被检测到;
  • 利用钩子参数优化性能:直接用钩子传递的$note_id和$order,只处理当前新增备注的订单,不用全局扫描;
  • 前置状态检查:先判断订单是否是processing状态,不符合直接返回,减少无效操作;
  • 添加状态更新说明:给update_status加了第二个参数,后台能看到状态更新的原因,方便溯源。

历史订单批量处理(可选)

如果之前已经有一批订单加了自动备注但没更新状态,可以用下面的代码批量处理一次,之后删掉即可:

// 批量处理历史订单(运行一次后删除)
add_action( 'admin_init', 'bulk_update_delhivery_orders' );
function bulk_update_delhivery_orders() {
    // 仅管理员可触发,防止误操作
    if ( ! current_user_can( 'manage_options' ) || ! isset( $_GET['bulk_delhivery_update'] ) ) {
        return;
    }

    $orders = wc_get_orders( array(
        'status' => 'processing',
        'limit'  => -1
    ) );

    foreach ( $orders as $order ) {
        // 获取该订单所有备注(不限制类型)
        $order_notes = wc_get_order_notes( array(
            'order_id' => $order->get_id(),
            'orderby'  => 'date_created_gmt'
        ) );

        foreach ( $order_notes as $note ) {
            $note_content = $note->content;
            $has_delhivery_info = (
                stripos( $note_content, 'SHIPMENT NUMBER :' ) !== false &&
                stripos( $note_content, 'SHIPPER : Delhivery' ) !== false &&
                stripos( $note_content, 'TRACKING : https://www.delhivery.com/track/package/' ) !== false
            );

            if ( $has_delhivery_info ) {
                error_log( '批量更新订单状态为已完成,订单ID: ' . $order->get_id() );
                $order->update_status( 'completed', '批量处理:检测到Delhivery发货信息' );
                break;
            }
        }
    }

    // 跳转回订单列表,避免重复执行
    wp_redirect( admin_url( 'edit.php?post_type=shop_order' ) );
    exit;
}

使用方法:登录后台后访问 你的站点域名/wp-admin/edit.php?post_type=shop_order&bulk_delhivery_update=1,执行完成后记得删除这段代码。

内容的提问来源于stack exchange,提问作者Saurabh Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:50:21