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

如何隐藏WooCommerce后台订单总额中的原始小计行?

WooCommerce后台订单总额自定义(隐藏/排序)问题解决

问题说明

  • 尝试隐藏WooCommerce后台订单里的小计(Subtotal),使用以下CSS代码无效:
.cart-subtotal  { display: none; }
.order-total { display: none;    }
.order_shipping_line_items {display: none;}
  • 改用PHP代码unset相关项也未生效:
add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display ){
    $shipping = $total_rows['line_subtotal'];
    $order_total = $total_rows['order_total'];

    unset($total_rows['line_subtotal']);
    unset($total_rows['order_total']);


    return $total_rows;
}
  • 需求:对订单总额区域的字段进行重新排序和隐藏,参考截图:
    订单小计区域截图

解决方案

1. PHP代码实现(推荐)

问题根源是使用了错误的数组键名,后台订单总额的小计键为subtotal而非line_subtotal,同时调整过滤器优先级确保覆盖默认设置:

add_filter( 'woocommerce_get_order_item_totals', 'customize_admin_order_totals', 99, 3 );
function customize_admin_order_totals( $total_rows, $order, $tax_display ){
    // 隐藏小计
    if ( isset( $total_rows['subtotal'] ) ) {
        unset( $total_rows['subtotal'] );
    }

    // 可选:隐藏订单总额
    // if ( isset( $total_rows['order_total'] ) ) {
    //     unset( $total_rows['order_total'] );
    // }

    // 示例:重新排序字段(将配送行移至最前)
    if ( isset( $total_rows['shipping'] ) ) {
        $shipping_row = $total_rows['shipping'];
        unset( $total_rows['shipping'] );
        $total_rows = array_merge( [ 'shipping' => $shipping_row ], $total_rows );
    }

    return $total_rows;
}

2. CSS方法(备用)

若PHP方法不生效,可针对后台订单页面注入CSS,注意后台元素的正确类名:

add_action( 'admin_head', 'custom_admin_order_css' );
function custom_admin_order_css() {
    global $pagenow;
    // 仅在订单编辑页面生效
    if ( $pagenow === 'post.php' && get_post_type() === 'shop_order' ) {
        echo '<style>
            .order_details tfoot tr.cart-subtotal { display: none !important; }
            /* 可选:隐藏订单总额 */
            /* .order_details tfoot tr.order-total { display: none !important; } */
        </style>';
    }
}

内容的提问来源于stack exchange,提问作者Victor Daniel Witoszek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 08:50:17