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

WooCommerce我的账户订单页新增订单总重量列开发求助

报错原因

$order->get_weight()触发错误的核心原因:get_weight()是WooCommerceWC_Product商品类的内置方法,WC_Order订单类无原生同名方法,直接调用会抛出方法不存在的致命错误。

实现方案

订单总重量无原生直接调用的方法,需要遍历订单内所有商品条目,累加「单商品重量 * 购买数量」计算得到。你可以根据需求选择以下两种实现方式:

方案1:单独新增「总重量」列

在现有代码基础上新增独立列展示订单总重量,完整可运行代码如下:

// 注册我的账户订单列表自定义列
add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];
    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;
        // 在订单状态列后依次插入商品信息列、总重量列
        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( '商品信息', 'woocommerce' );
            $new_columns['order-weight'] = __( '总重量', 'woocommerce' );
        }
    }
    return $new_columns;
}

// 渲染商品信息列内容
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'render_order_items_column_content', 10, 1 );
function render_order_items_column_content( $order ) {
    $details = [];
    foreach( $order->get_items() as $item ) {
        $details[] = $item->get_name() . ' × ' . $item->get_quantity();
    }
    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}

// 渲染总重量列内容
add_action( 'woocommerce_my_account_my_orders_column_order-weight', 'render_order_weight_column_content', 10, 1 );
function render_order_weight_column_content( $order ) {
    $total_weight = 0;
    // 遍历所有订单项累加重量
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        // 跳过已删除、未设置重量的商品
        if ( !$product || !$product->get_weight() ) continue;
        $total_weight += (float)$product->get_weight() * $item->get_quantity();
    }
    echo $total_weight > 0 ? wc_format_weight( $total_weight ) : '&ndash;';
}

方案2:在现有商品信息列追加总重量

如果不需要单独展示重量列,可以直接在现有商品信息列底部追加总重量行,仅需修改列渲染回调函数即可:

// 渲染商品信息列(追加总重量)
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'render_order_items_column_content', 10, 1 );
function render_order_items_column_content( $order ) {
    $details = [];
    $total_weight = 0;
    foreach( $order->get_items() as $item ) {
        $details[] = $item->get_name() . '&nbsp;&times;&nbsp;' . $item->get_quantity();
        // 同步计算总重量
        $product = $item->get_product();
        if ( $product && $product->get_weight() ) {
            $total_weight += (float)$product->get_weight() * $item->get_quantity();
        }
    }
    // 追加总重量展示行
    if ( $total_weight > 0 ) {
        $details[] = sprintf( '<small>%s:%s</small>', __( '总重量', 'woocommerce' ), wc_format_weight( $total_weight ) );
    }
    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}
兼容说明
  • 重量输出调用wc_format_weight()函数,会自动读取站点后台设置的重量单位(kg/g/磅等),无需手动硬编码单位
  • 已做异常兼容:商品被删除、商品未设置重量时不会触发报错,自动跳过对应项计算
  • 天然兼容多规格商品:会自动读取用户实际购买规格对应的重量,无需额外处理变体商品逻辑
  • 重量计算逻辑和WooCommerce购物车、订单结算页的重量计算规则一致,不会出现数值偏差

内容的提问来源于stack exchange,提问作者go-diego-go

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.31 12:00:56