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

如何将自定义WooCommerce订单状态添加到批量操作菜单与顶部栏

你当前的代码仅完成了自定义「Delivered(已交付)」订单状态的基础注册,WooCommerce不会自动将自定义状态同步到顶部筛选栏和批量操作下拉列表,需要补充对应钩子代码实现需求。

补充代码(直接追加到你现有代码的末尾即可)

1. 添加到订单页顶部状态筛选栏

// 将自定义订单状态添加到订单页顶部筛选栏
add_filter( 'views_edit-shop_order', 'add_delivered_status_to_top_bar' );
function add_delivered_status_to_top_bar( $views ) {
    global $wpdb;
    // 统计已交付状态的订单数量
    $count = $wpdb->get_var( "
        SELECT COUNT(ID) FROM {$wpdb->posts} 
        WHERE post_type = 'shop_order' 
        AND post_status = 'wc-delivered'
    " );
    // 拼接新的筛选选项
    $views['wc-delivered'] = sprintf(
        '<a href="%s">%s <span class="count">(%d)</span></a>',
        admin_url( 'edit.php?post_type=shop_order&post_status=wc-delivered' ),
        'Delivered',
        $count
    );
    return $views;
}

2. 添加到批量操作下拉列表并实现处理逻辑

// 将自定义订单状态添加到批量操作下拉列表
add_filter( 'bulk_actions-edit-shop_order', 'add_delivered_status_to_bulk_actions' );
function add_delivered_status_to_bulk_actions( $bulk_actions ) {
    $bulk_actions['mark_delivered'] = '标记为已交付';
    // 如需显示英文可改为 'Mark as Delivered'
    return $bulk_actions;
}

// 处理批量标记为已交付的操作逻辑
add_filter( 'handle_bulk_actions-edit-shop_order', 'handle_bulk_mark_delivered', 10, 3 );
function handle_bulk_mark_delivered( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'mark_delivered' ) {
        return $redirect_to;
    }
    // 遍历选中的订单修改状态
    foreach ( $post_ids as $post_id ) {
        $order = wc_get_order( $post_id );
        $order->update_status( 'delivered', '批量操作更新为已交付状态' );
    }
    // 返回时添加成功参数,用于显示操作提示
    $redirect_to = add_query_arg( 'bulk_mark_delivered', count( $post_ids ), $redirect_to );
    return $redirect_to;
}

// 批量操作成功后的提示
add_action( 'admin_notices', 'bulk_delivered_success_notice' );
function bulk_delivered_success_notice() {
    if ( ! empty( $_REQUEST['bulk_mark_delivered'] ) ) {
        $changed_count = intval( $_REQUEST['bulk_mark_delivered'] );
        printf(
            '<div id="message" class="updated notice is-dismissible"><p>已将 %d 个订单标记为已交付状态</p></div>',
            $changed_count
        );
    }
}

生效方法

将以上代码追加到你当前使用的主题functions.php文件或者自定义功能插件中,保存后刷新WooCommerce订单后台即可看到对应选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 04:27:04