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

在WooCommerce中添加带标题的邮件自定义元数据表格及后台订单列

Hey there! Let's break down how to tweak your existing WooCommerce code to hit both of your requirements. Here's the step-by-step solution:

1. Add Heading & Table Wrapper to WooCommerce Email Order Meta

First, we'll modify the function that outputs your custom attendee fields in order emails. We'll add a clear heading and wrap the fields in an HTML table for cleaner formatting (works for HTML emails, which is the default for WooCommerce):

add_action( 'woocommerce_email_order_meta', 'display_attendee_info_in_emails', 20, 3 );
function display_attendee_info_in_emails( $order, $sent_to_admin, $plain_text ) {
    // Skip plain-text emails since we're using HTML formatting
    if ( $plain_text ) return;

    // Replace '_your_attendee_meta_key' with your actual custom field meta key
    $attendee_data = $order->get_meta( '_your_attendee_meta_key' );

    // Only proceed if we have valid attendee data
    if ( ! empty( $attendee_data ) && is_array( $attendee_data ) ) {
        // Output the section heading
        echo '<h2>Attendee Info</h2>';
        
        // Start the table with basic inline CSS for email client compatibility
        echo '<table style="width: 100%; margin: 1em 0; border-collapse: collapse;">';
        
        // Loop through each attendee field and render table rows
        foreach ( $attendee_data as $field_label => $field_value ) {
            // Sanitize values to prevent HTML issues
            $safe_label = esc_html( $field_label );
            $safe_value = esc_html( $field_value );
            
            echo '<tr>
                    <td style="border: 1px solid #eee; padding: 8px; font-weight: bold;">' . $safe_label . '</td>
                    <td style="border: 1px solid #eee; padding: 8px;">' . $safe_value . '</td>
                  </tr>';
        }
        
        // Close the table
        echo '</table>';
    }
}

Pro tip: Adjust the inline CSS in the table to match your store's email branding if needed!

2. Add Custom Column to WooCommerce Admin Order List

Next, we'll add a new column to the orders list in your WordPress admin, and populate it with the specific attendee fields you want to display. This requires two hooks: one to register the column, and another to fill its content.

Step 2.1: Register the New Column

This code adds the "Attendee Info" column right after the "Total" column (you can adjust the position by changing the comparison key):

add_filter( 'manage_edit-shop_order_columns', 'add_attendee_admin_column' );
function add_attendee_admin_column( $columns ) {
    $updated_columns = array();
    foreach ( $columns as $key => $label ) {
        $updated_columns[ $key ] = $label;
        // Insert new column after the "Total" column
        if ( $key === 'order_total' ) {
            $updated_columns['attendee_info'] = 'Attendee Info';
        }
    }
    return $updated_columns;
}

Step 2.2: Populate the Column with Specific Fields

This function pulls the relevant attendee data and displays only the fields you specify:

add_action( 'manage_shop_order_posts_custom_column', 'populate_attendee_admin_column', 10, 2 );
function populate_attendee_admin_column( $column, $post_id ) {
    if ( $column === 'attendee_info' ) {
        $order = wc_get_order( $post_id );
        // Again, replace with your actual meta key
        $attendee_data = $order->get_meta( '_your_attendee_meta_key' );

        if ( ! empty( $attendee_data ) && is_array( $attendee_data ) ) {
            // List the specific fields you want to show here
            $fields_to_display = array( 'Full Name', 'Email Address', 'Ticket Type' );
            
            foreach ( $fields_to_display as $field_key ) {
                if ( isset( $attendee_data[ $field_key ] ) ) {
                    echo '<strong>' . esc_html( $field_key ) . ':</strong> ' . esc_html( $attendee_data[ $field_key ] ) . '<br>';
                }
            }
        } else {
            echo 'No attendee data';
        }
    }
}

Just update $fields_to_display with the exact field names from your custom array to show only what's relevant in the admin.

内容的提问来源于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:05:39