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

如何在WooCommerce中提取自定义字段数组值用于邮件及后台

解决FooEvents参会者信息在WooCommerce中的展示问题

首先,我们需要先正确解析WooCommerceEventsOrderTickets元数据里的数组(你已经通过print_r确认了解构后的结构),然后提取你需要的字段,再分别应用到订单邮件、后台订单列表和订单详情页中。

1. 修改订单邮件代码,展示指定参会者信息

我们需要先获取并解析订单元数据,考虑到可能存在多张票/多个参会者的情况,代码会自动遍历所有参会者,提取你需要的字段添加到邮件里:

/**
 * 在WooCommerce订单邮件中添加FooEvents参会者信息
 */
add_filter( 'woocommerce_email_order_meta_fields', 'add_fooevents_attendee_info_to_emails', 10, 3 );
function add_fooevents_attendee_info_to_emails( $fields, $sent_to_admin, $order ) {
    // 获取参会者元数据
    $tickets_meta = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );
    
    // 检查元数据是否有效
    if ( is_array( $tickets_meta ) && !empty( $tickets_meta ) ) {
        // 遍历票组与参会者
        foreach ( $tickets_meta as $ticket_group ) {
            foreach ( $ticket_group as $attendee ) {
                // 提取基础参会者姓名
                $fields['attendee_full_name'] = array(
                    'label' => __( '参会者姓名' ),
                    'value' => $attendee['WooCommerceEventsAttendeeName'] . ' ' . $attendee['WooCommerceEventsAttendeeLastName'],
                );
                
                // 提取自定义字段信息
                $custom_fields = $attendee['WooCommerceEventsCustomAttendeeFields'];
                $fields['attendee_address'] = array(
                    'label' => __( '参会者地址' ),
                    'value' => $custom_fields['fooevents_custom_address'],
                );
                $fields['attendee_state'] = array(
                    'label' => __( '州/省份' ),
                    'value' => $custom_fields['fooevents_custom_state/province'],
                );
                $fields['attendee_title'] = array(
                    'label' => __( '职位' ),
                    'value' => $custom_fields['fooevents_custom_title'],
                );
            }
        }
    }
    
    return $fields;
}

2. 在后台订单列表新增参会者信息列

我们需要先注册一个新列,再填充对应内容:

/**
 * 给WooCommerce订单列表添加参会者姓名列
 */
add_filter( 'manage_edit-shop_order_columns', 'add_attendee_name_order_column' );
function add_attendee_name_order_column( $columns ) {
    // 在订单状态列后插入新列
    $columns['attendee_name'] = __( '参会者姓名', 'woocommerce' );
    return $columns;
}

/**
 * 填充参会者姓名列的内容
 */
add_action( 'manage_shop_order_posts_custom_column', 'fill_attendee_name_order_column', 10, 2 );
function fill_attendee_name_order_column( $column, $post_id ) {
    if ( $column === 'attendee_name' ) {
        $tickets_meta = get_post_meta( $post_id, 'WooCommerceEventsOrderTickets', true );
        if ( is_array( $tickets_meta ) && !empty( $tickets_meta ) ) {
            $attendee_names = array();
            foreach ( $tickets_meta as $ticket_group ) {
                foreach ( $ticket_group as $attendee ) {
                    $full_name = $attendee['WooCommerceEventsAttendeeName'] . ' ' . $attendee['WooCommerceEventsAttendeeLastName'];
                    $attendee_names[] = $full_name;
                }
            }
            // 多个参会者用逗号分隔展示
            echo implode( ', ', $attendee_names );
        } else {
            echo '-';
        }
    }
}

3. 在订单详情页的账单信息下方展示参会者详情

利用WooCommerce的钩子,在订单后台详情页的账单区域下方添加自定义参会者信息板块:

/**
 * 在订单详情页账单信息下方添加FooEvents参会者详情
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_attendee_details_in_order_admin', 10, 1 );
function display_attendee_details_in_order_admin( $order ) {
    $tickets_meta = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );
    if ( is_array( $tickets_meta ) && !empty( $tickets_meta ) ) {
        echo '<div class="attendee-details">';
        echo '<h3>' . __( '参会者信息', 'woocommerce' ) . '</h3>';
        echo '<table class="widefat fixed" cellspacing="0">';
        echo '<thead><tr><th>' . __( '姓名' ) . '</th><th>' . __( '地址' ) . '</th><th>' . __( '州/省份' ) . '</th><th>' . __( '职位' ) . '</th></tr></thead>';
        echo '<tbody>';
        
        foreach ( $tickets_meta as $ticket_group ) {
            foreach ( $ticket_group as $attendee ) {
                $custom_fields = $attendee['WooCommerceEventsCustomAttendeeFields'];
                echo '<tr>';
                echo '<td>' . $attendee['WooCommerceEventsAttendeeName'] . ' ' . $attendee['WooCommerceEventsAttendeeLastName'] . '</td>';
                echo '<td>' . $custom_fields['fooevents_custom_address'] . '</td>';
                echo '<td>' . $custom_fields['fooevents_custom_state/province'] . '</td>';
                echo '<td>' . $custom_fields['fooevents_custom_title'] . '</td>';
                echo '</tr>';
            }
        }
        
        echo '</tbody></table>';
        echo '</div>';
    }
}

小提示

  • 你可以根据需求修改代码中的字段标签(比如换成英文或其他语言)
  • 如果订单存在多个参会者,上述代码会自动遍历并展示所有参会者信息
  • 建议在测试环境先验证代码,不同版本的FooEvents可能会有元键的细微差异

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:02:56