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

如何合并WooCommerce脚本:后台订单列表显示已保存重量

问题:WooCommerce订单管理页显示已保存的订单重量,避免每次加载重新计算

我在WooCommerce站点的订单管理页面使用脚本计算并显示每个订单的重量,但每次页面加载都要重新计算。我找到了保存订单重量的脚本,但自己不会合并,希望实现显示已保存的订单重量,而非每次加载都重新计算,求正确的合并方案。

原保存订单重量的脚本:

add_action( 'woocommerce_checkout_update_order_meta', 'solhatt_save_weight_order' );
 
function solhatt_save_weight_order( $order_id ) {
    $weight = WC()->cart->get_cart_contents_weight();
    update_post_meta( $order_id, '_cart_weight', $weight );
}

原计算并显示订单重量的脚本:

add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
  $columns['total_weight'] = __( 'Weight', 'woocommerce' );
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
    global $post, $woocommerce, $the_order;

    if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
        $the_order = new WC_Order( $post->ID );

    if ( $column == 'total_weight' ) {
        $weight = 0;
        if ( sizeof( $the_order->get_items() ) > 0 ) {
            foreach( $the_order->get_items() as $item ) {
                if ( $item['product_id'] > 0 ) {
                    $_product = $item->get_product();
                    if ( ! $_product->is_virtual() ) {
                        $weight += $_product->get_weight() * $item['qty'];
                    }
                }
            }
        }
        if ( $weight > 0 ) {
            print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
        } else {
            print 'N/A';
        }
    }
}

合并后的脚本
// 结账时保存订单总重量到订单元数据
add_action( 'woocommerce_checkout_update_order_meta', 'solhatt_save_weight_order' );
function solhatt_save_weight_order( $order_id ) {
    $weight = WC()->cart->get_cart_contents_weight();
    update_post_meta( $order_id, '_cart_weight', $weight );
}

// 在订单管理页添加重量列
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
    $columns['total_weight'] = __( 'Weight', 'woocommerce' );
    return $columns;
}

// 显示订单重量:优先读取已保存的元数据,旧订单自动计算并保存
add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
    global $post, $the_order;

    // 初始化订单对象(使用WooCommerce推荐的函数)
    if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
        $the_order = wc_get_order( $post->ID );
    }

    if ( $column == 'total_weight' ) {
        $weight_unit = esc_attr( get_option('woocommerce_weight_unit') );
        // 先尝试读取已保存的重量数据
        $saved_weight = get_post_meta( $the_order->get_id(), '_cart_weight', true );

        if ( ! empty( $saved_weight ) && $saved_weight > 0 ) {
            echo $saved_weight . ' ' . $weight_unit;
        } else {
            // 无保存数据时,计算订单总重量
            $weight = 0;
            if ( $items = $the_order->get_items() ) {
                foreach( $items as $item ) {
                    $product = $item->get_product();
                    // 跳过虚拟商品,累加商品重量×数量
                    if ( $product && ! $product->is_virtual() ) {
                        $product_weight = (float) $product->get_weight();
                        $weight += $product_weight * $item->get_quantity();
                    }
                }
            }

            if ( $weight > 0 ) {
                echo $weight . ' ' . $weight_unit;
                // 将计算结果保存到元数据,避免下次重复计算
                update_post_meta( $the_order->get_id(), '_cart_weight', $weight );
            } else {
                echo 'N/A';
            }
        }
    }
}

关键修改说明
  1. 保留结账保存逻辑:新订单在完成结账时,会自动将购物车总重量保存到订单的_cart_weight元数据中
  2. 优先读取已保存数据:订单管理页加载时,先读取已保存的重量,无需重复计算
  3. 兼容旧订单:对于没有保存重量的旧订单,会自动计算一次并保存,后续加载直接读取
  4. 代码优化:使用wc_get_order()替代旧的new WC_Order(),用get_quantity()替代直接访问数组键,提升代码与WooCommerce新版本的兼容性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 08:25:14