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

如何在WooCommerce后台新订单通知中添加商品每千克单价

解决WooCommerce后台新订单显示商品每千克单价的问题

获取商品小计的方法

你可以直接通过订单项对象调用get_subtotal()方法获取对应商品的小计,这个值就是截图里的「82,80」:

$subtotal = $item->get_subtotal();

方案1:仅在admin-new-order.php中显示(推荐)

找到admin-new-order.php里循环订单项的代码块(通常是foreach ( $order->get_items() as $item_id => $item )这段),插入以下代码:

// 获取产品信息、重量、数量和小计
$product = $item->get_product();
$product_weight = $product->get_weight();
$qty = $item->get_quantity();
$subtotal = $item->get_subtotal();

// 计算并输出每千克单价(添加判断避免除以0报错)
if ( $product_weight && $qty > 0 && $subtotal > 0 ) {
    $price_per_kg = $subtotal / $product_weight / $qty;
    // 用WooCommerce内置函数适配货币格式输出
    echo wc_price( $price_per_kg );
}

方案2:在email-order-items.php中添加(同时显示在后台和客户邮件)

如果只能通过邮件模板实现,可在email-order-items.php的订单项循环里加代码,并通过邮件ID判断仅在后台新订单邮件中显示:

$product = $item->get_product();
$product_weight = $product->get_weight();
$qty = $item->get_quantity();
$subtotal = $item->get_subtotal();

// 仅针对后台新订单邮件显示
if ( $email->id === 'new_order' && $product_weight && $qty > 0 && $subtotal > 0 ) {
    $price_per_kg = $subtotal / $product_weight / $qty;
    echo '<br>每千克单价:' . wc_price( $price_per_kg );
}

注意事项

  • 使用wc_price()可以自动匹配你的WooCommerce货币显示规则(比如逗号分隔小数)
  • 必须添加重量、数量、小计的非空/非零判断,避免出现PHP警告或计算错误
  • 修改模板前建议在子主题中复制对应模板文件再修改,防止主题更新覆盖自定义内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 09:12:36