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

在WooCommerce邮件通知中添加不含税订单项价格列

在WooCommerce订单邮件订单项表格添加不含税价格列(无需自定义模板)

需求说明

需在WooCommerce新订单邮件的订单项表格中新增一列,展示每个商品的不含税价格。目前已通过过滤器实现订单总额区域的不含税总额行,但希望不修改自定义邮件模板(减少版本升级时的维护工作量),仅通过钩子实现订单项的不含税价格列。

当前使用的默认邮件模板代码

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer billing full name */ ?>
<p><?php printf( esc_html__( 'You’ve received the following order from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

已实现的总额不含税代码

// 为订单邮件添加不含税总额行
add_filter( 'woocommerce_get_order_item_totals', 'add_order_total_excl_vat_row', 10, 3 );
function add_order_total_excl_vat_row( $total_rows, $order, $tax_display ) {
    if( ! is_wc_endpoint_url() || ! is_admin() ) {

        // 暂存并移除原有总计行
        $gran_total = $total_rows['order_total'];
        unset( $total_rows['order_total'] );

        // 插入不含税总计行
        $total_rows['order_total_ev'] = array(
            'label' => __( 'Total Excl. VAT :', 'woocommerce' ),
            'value' => wc_price( $order->get_total() - $order->get_total_tax() ),
        );

        // 恢复原有总计行
        $total_rows['order_total'] = $gran_total;
    }
    return $total_rows;
}

解决方案:通过钩子实现订单项不含税价格列

以下代码无需修改邮件模板,仅利用WooCommerce自带的过滤器和动作钩子即可实现需求:

1. 添加不含税价格列表头

// 为订单邮件订单项表格添加新列表头
add_filter( 'woocommerce_email_order_items_columns', 'add_email_order_items_price_excl_tax_column', 10 );
function add_email_order_items_price_excl_tax_column( $columns ) {
    // 在商品名称列后插入新列(可根据需求调整插入位置)
    $new_columns = array();
    foreach ( $columns as $key => $label ) {
        $new_columns[ $key ] = $label;
        // 匹配商品名称列的键,插入新列
        if ( 'order_item' === $key ) {
            $new_columns['price_excl_tax'] = __( '不含税价格', 'woocommerce' );
        }
    }
    return $new_columns;
}

2. 填充不含税价格列内容

// 为订单项表格的新列填充不含税价格
add_action( 'woocommerce_email_order_item_values', 'add_email_order_items_price_excl_tax_value', 10, 3 );
function add_email_order_items_price_excl_tax_value( $item, $item_id, $order ) {
    // 仅在邮件环境中生效,避免影响前端订单页面
    if ( ! is_email() ) return;

    // 计算商品不含税单价(小计除以数量)
    $price_excl_tax = $item->get_subtotal() / $item->get_quantity();
    ?>
    <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
        <?php echo wc_price( $price_excl_tax, array( 'currency' => $order->get_currency() ) ); ?>
    </td>
    <?php
}

代码说明

  • 表头部分通过woocommerce_email_order_items_columns过滤器修改邮件订单项表格的列结构,可根据需求调整新列的插入位置。
  • 内容部分通过woocommerce_email_order_item_values动作,为每个订单项输出对应的不含税单价,样式与原表格保持一致。
  • 加入is_email()判断,确保该列仅在邮件中显示,不会干扰前端订单详情页面的展示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 12:24:59