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

WooCommerce后台高亮指定配送方式订单:同环境代码迁移失效排查

问题描述

在相同WooCommerce版本、PHP版本、主题及主机环境下,将某一站点可用的「后台高亮自提(Local Pickup)订单」代码复制到另一站点后失效,尝试了自身代码及相关方案均无效果。

原代码如下:

// **********local pickup orders list color**********

function filter_post_class( $classes, $class, $post_id ) {
    // Determines whether the current request is for an administrative interface page
    if ( ! is_admin() ) return $classes;

    // Get the current screen object
    $current_screen = get_current_screen();

    // Only when
    if ( $current_screen->id === 'edit-shop_order' ) {
        // Get an instance of the WC_Order object
        $order = wc_get_order( $post_id );

        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Get the shipping method
            // $shipping_method = $order->get_shipping_methods();
            $shipping_method = @array_shift($order->get_shipping_methods());
            $shipping_method_id = $shipping_method['method_id'];

             //NOT empty
            if ( ! empty( $shipping_method ) ) {
                $classes[] = $shipping_method_id;
            }
        }
    }

    // Return the array
    return $classes;
}
add_filter( 'post_class', 'filter_post_class', 10, 3 );

// Add CSS
function action_admin_head() {
    // Get the current screen object
    $current_screen = get_current_screen();

    // Only when
    if ( $current_screen->id === 'edit-shop_order' ) {
        echo '<style>
            .type-shop_order.local_pickup {
                background-color: #d5d6ad !important;
            }

        </style>';
    }
}
add_action( 'admin_head', 'action_admin_head' );
可能的失效原因
  • 自提方式ID不匹配:不同站点的自提配送方式ID不一定是local_pickup,比如使用自定义自提插件时,ID可能变成local_pickup_plus这类自定义值。
  • 错误抑制符隐藏问题:代码里用了@array_shift,如果订单无配送方式(如虚拟商品订单),会屏蔽错误,但后续获取$shipping_method['method_id']时会触发致命错误,导致函数中断,无法添加CSS类。
  • 订单权限限制:目标站点可能开启了订单权限控制,当前管理员账号没有读取订单配送信息的权限,导致wc_get_order无法正确获取订单对象。
  • CSS优先级冲突:目标站点后台原有订单行背景样式优先级更高,覆盖了自定义的!important规则。
修复方案

1. 确认自提方式的真实ID

在目标站点的WooCommerce设置→配送→自提方式中,通过浏览器开发者工具查看URL参数,或者添加以下代码输出所有配送方式ID:

add_action( 'admin_init', 'check_shipping_method_ids' );
function check_shipping_method_ids() {
    $shipping_methods = WC()->shipping->get_shipping_methods();
    foreach ( $shipping_methods as $method ) {
        error_log( 'Shipping Method ID: ' . $method->id );
    }
}

添加到主题functions.php后,访问后台任意页面,查看服务器错误日志获取真实ID,替换代码中的local_pickup。

2. 修复错误抑制符问题

移除@符号,添加安全判断避免函数崩溃:

// 修改原代码中获取配送方式的部分
$shipping_methods = $order->get_shipping_methods();
if ( ! empty( $shipping_methods ) ) {
    $shipping_method = array_shift( $shipping_methods );
    if ( isset( $shipping_method['method_id'] ) ) {
        $shipping_method_id = $shipping_method['method_id'];
        $classes[] = $shipping_method_id;
    }
}

3. 验证订单权限

切换到超级管理员账号测试代码,确认当前账号拥有「查看所有订单」的权限。

4. 提升CSS优先级

增强选择器优先级,确保覆盖原有样式:

.wp-list-table.widefat.fixed.striped.posts .type-shop_order.local_pickup {
    background-color: #d5d6ad !important;
}

完整修复后的代码

// 后台高亮自提订单
function filter_post_class( $classes, $class, $post_id ) {
    if ( ! is_admin() ) return $classes;

    $current_screen = get_current_screen();
    if ( $current_screen->id !== 'edit-shop_order' ) return $classes;

    $order = wc_get_order( $post_id );
    if ( ! is_a( $order, 'WC_Order' ) ) return $classes;

    $shipping_methods = $order->get_shipping_methods();
    if ( ! empty( $shipping_methods ) ) {
        $shipping_method = array_shift( $shipping_methods );
        if ( isset( $shipping_method['method_id'] ) ) {
            $classes[] = $shipping_method['method_id'];
        }
    }

    return $classes;
}
add_filter( 'post_class', 'filter_post_class', 10, 3 );

// 添加高亮样式
function action_admin_head() {
    $current_screen = get_current_screen();
    if ( $current_screen->id !== 'edit-shop_order' ) return;

    echo '<style>
        .wp-list-table.widefat.fixed.striped.posts .type-shop_order.local_pickup {
            background-color: #d5d6ad !important;
        }
    </style>';
}
add_action( 'admin_head', 'action_admin_head' );

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 14:39:50