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

如何在Woocommerce后台订单页及邮件中显示动态自定义字段值?

没问题,我来帮你搞定这两个需求——后台订单编辑页展示自定义参会者字段,还有把这些信息加到订单邮件里。咱们一步步来:

一、在后台订单编辑页面展示自定义字段

我们可以利用WooCommerce的woocommerce_admin_order_data_after_billing_address钩子,把参会者信息插入到订单编辑页的账单地址下方。核心思路是循环读取你之前保存的订单元数据,直到找不到对应索引的字段为止:

// 在后台订单编辑页的账单地址下方添加参会者信息
add_action('woocommerce_admin_order_data_after_billing_address', 'display_attendee_fields_in_admin_order', 10, 1);
function display_attendee_fields_in_admin_order($order) {
    echo '<div class="attendee-info" style="margin-top:20px;padding-top:20px;border-top:1px solid #eee;">';
    echo '<h4>' . __('参会者信息', 'woocommerce') . '</h4>';
    
    // 循环遍历所有参会者字段(索引从1开始)
    $index = 1;
    while (true) {
        // 获取对应索引的参会者姓名和出发地
        $attendee_name = $order->get_meta('attendee_name_' . $index, true);
        $departure_location = $order->get_meta('departure_location_' . $index, true);
        
        // 如果两个字段都为空,说明没有更多参会者,终止循环
        if (empty($attendee_name) && empty($departure_location)) {
            break;
        }
        
        // 输出单个参会者的信息
        echo '<p><strong>' . __('参会者 ' . $index, 'woocommerce') . ':</strong></p>';
        echo '<p>' . __('姓名:', 'woocommerce') . ' ' . esc_html($attendee_name) . '</p>';
        echo '<p>' . __('出发地:', 'woocommerce') . ' ' . esc_html($departure_location) . '</p>';
        echo '<hr style="margin:15px 0;">';
        
        $index++;
    }
    
    echo '</div>';
}

注意:如果你的自定义字段名不是attendee_name_{index}和departure_location_{index},一定要把代码里的字段名替换成你实际使用的名称(比如你用的是participant_name_1,就改成对应的)。

二、在订单邮件通知中展示自定义字段

我们用woocommerce_email_after_order_table钩子,把参会者信息添加到邮件的订单商品表格之后。你还可以控制是否只在客户邮件里显示,或者管理员邮件也一并展示:

// 在订单邮件的订单表格后添加参会者信息
add_action('woocommerce_email_after_order_table', 'display_attendee_fields_in_order_email', 10, 4);
function display_attendee_fields_in_order_email($order, $sent_to_admin, $plain_text, $email) {
    // 可选:取消下面的注释,只在客户收到的邮件里显示参会者信息
    // if ($sent_to_admin) return;
    
    echo '<h2 style="margin:20px 0;font-size:1.2em;">' . __('参会者信息', 'woocommerce') . '</h2>';
    echo '<ul style="list-style:none;padding-left:0;margin:0;">';
    
    $index = 1;
    while (true) {
        $attendee_name = $order->get_meta('attendee_name_' . $index, true);
        $departure_location = $order->get_meta('departure_location_' . $index, true);
        
        if (empty($attendee_name) && empty($departure_location)) {
            break;
        }
        
        echo '<li style="margin-bottom:15px;padding-bottom:15px;border-bottom:1px solid #eee;">';
        echo '<strong>' . __('参会者 ' . $index, 'woocommerce') . '</strong><br>';
        echo __('姓名:', 'woocommerce') . ' ' . esc_html($attendee_name) . '<br>';
        echo __('出发地:', 'woocommerce') . ' ' . esc_html($departure_location);
        echo '</li>';
        
        $index++;
    }
    
    echo '</ul>';
}

关键提示

  1. 确保你之前保存字段时,是用update_post_meta()把数据存在了订单的元数据里(WooCommerce订单本质是自定义文章类型,元数据就是post meta),这样$order->get_meta()才能正确读取到数据。
  2. 把这些代码添加到你的主题functions.php文件中,或者用专门的自定义代码插件(比如Code Snippets)来添加,不要直接修改WooCommerce核心文件。
  3. 如果你的字段命名规则和示例不同,一定要同步修改代码里的字段名,否则会读取不到数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:19:20