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

为特定角色在WooCommerce我的账户订单页添加搜索功能遇阻

WooCommerce账户订单页搜索栏失效问题排查

问题现象

  • 为管理员、店铺经理角色在前端账户订单页添加的订单号搜索栏,提交搜索后显示“No order has been made yet.”,但测试账户存在大量订单
  • 搜索操作后页面URL无任何变化,不符合预期

用户提供的代码

function add_search_to_orders() {
    $allowed_roles = array('administrator', 'shop_manager');

    $user = wp_get_current_user();
    $user_roles = (array) $user->roles;

    if (array_intersect($allowed_roles, $user_roles)) {
        echo '<form method="post" id="orders-search-form">
                <input type="text" name="search_orders" placeholder="Search by order number..." />
                <input type="hidden" name="order_status" value="wc-completed" />
                <button type="submit">Search</button>
              </form>';
    }
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');

function search_all_orders_by_number($args) {
    if (isset($_POST['search_orders']) && !empty($_POST['search_orders'])) {
        $search_term = sanitize_text_field($_POST['search_orders']);
        $args['meta_query'] = array(
            array(
                'key'     => '_order_number',
                'value'   => $search_term,
                'compare' => 'LIKE'
            )
        );
    }
    return $args;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');

问题原因分析

  1. 提交方式与查询逻辑不兼容:表单用POST提交,但woocommerce_my_account_my_orders_query钩子的查询依赖GET参数,POST提交后刷新页面会丢失参数,导致URL无变化,同时查询条件无法持久生效。
  2. 订单号元键错误:WooCommerce默认不使用_order_number存储订单号,默认订单ID就是前台显示的订单号;如果没有安装自定义订单号插件,这个元键不存在,自然查不到结果。
  3. 覆盖原有查询条件:直接赋值$args['meta_query']会替换掉WooCommerce原本的订单查询条件(比如用户ID筛选),导致查询范围变成所有订单而非当前用户的订单,最终无匹配结果。
  4. 强制限定订单状态:隐藏字段固定设置order_status为wc-completed,如果搜索的订单不是已完成状态,会直接被过滤掉。

修复后的代码

方案1:基于默认订单ID(无自定义订单号插件)

function add_search_to_orders() {
    $allowed_roles = array('administrator', 'shop_manager');
    $user = wp_get_current_user();
    $user_roles = (array) $user->roles;

    if (array_intersect($allowed_roles, $user_roles)) {
        // 改用GET提交,保留搜索参数在URL中
        $current_search = isset($_GET['search_orders']) ? sanitize_text_field($_GET['search_orders']) : '';
        echo '<form method="get" id="orders-search-form">
                <input type="text" name="search_orders" placeholder="按订单号搜索..." value="' . esc_attr($current_search) . '" />
                <!-- 保留原有状态筛选,不强制固定为已完成 -->
                <input type="hidden" name="page" value="orders" />
                <button type="submit">搜索</button>
              </form>';
    }
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');

function search_all_orders_by_number($args) {
    if (isset($_GET['search_orders']) && !empty($_GET['search_orders'])) {
        $order_id = absint($_GET['search_orders']);
        if ($order_id > 0) {
            // 直接筛选当前用户的指定订单ID
            $args['post__in'] = array($order_id);
        }
    }
    return $args;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');

方案2:使用自定义订单号(需确认元键正确)

如果确实使用了自定义订单号插件,先通过数据库确认存储订单号的元键(比如部分插件用_custom_order_number),再修改查询逻辑:

function add_search_to_orders() {
    $allowed_roles = array('administrator', 'shop_manager');
    $user = wp_get_current_user();
    $user_roles = (array) $user->roles;

    if (array_intersect($allowed_roles, $user_roles)) {
        $current_search = isset($_GET['search_orders']) ? sanitize_text_field($_GET['search_orders']) : '';
        echo '<form method="get" id="orders-search-form">
                <input type="text" name="search_orders" placeholder="按订单号搜索..." value="' . esc_attr($current_search) . '" />
                <input type="hidden" name="page" value="orders" />
                <button type="submit">搜索</button>
              </form>';
    }
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');

function search_all_orders_by_number($args) {
    if (isset($_GET['search_orders']) && !empty($_GET['search_orders'])) {
        $search_term = sanitize_text_field($_GET['search_orders']);
        // 合并原有meta_query,不覆盖
        if (!isset($args['meta_query'])) {
            $args['meta_query'] = array();
        }
        $args['meta_query'][] = array(
            'key'     => '_custom_order_number', // 替换为实际的自定义订单号元键
            'value'   => $search_term,
            'compare' => '=' // 订单号精确匹配,不用LIKE
        );
    }
    return $args;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');

内容的提问来源于stack exchange,提问作者Two Pines Brian

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:50:13