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

在WooCommerce中获取课程产品自定义字段值作为变量

WooCommerce课程注册:自定义字段转变量实现精准页面/邮件定制

刚好帮你搞定这个课程注册场景下的自定义字段需求——既然你已经搞定了购物车页的显示,接下来咱们把这些自定义字段转成独立变量,精准控制结账页、订单详情页和客户邮件的内容输出,完全避开批量钩子输出元数据的混乱问题。

核心思路:先把自定义字段转为可调用变量

不管是在结账、订单详情还是邮件里,咱们先把每个自定义字段的值提取成单独变量,这样就能随心所欲控制输出位置和格式。

1. 确保自定义字段已保存到订单元数据

首先得把购物车页的自定义字段值同步保存到订单里(不然订单里拿不到数据),用这个钩子:

add_action( 'woocommerce_checkout_create_order_line_item', 'save_course_fields_to_order', 10, 4 );
function save_course_fields_to_order( $item, $cart_item_key, $values, $order ) {
    // 替换成你的实际字段名,比如course_participant_name、course_session_time等
    $course_fields = [
        'course_participant_name',
        'course_participant_age',
        'course_session_time',
        // 最多添加到20个字段
    ];

    foreach ( $course_fields as $field_key ) {
        if ( isset( $values[$field_key] ) && !empty( $values[$field_key] ) ) {
            $item->add_meta_data( $field_key, $values[$field_key], true );
        }
    }
}

2. 提取字段为独立变量

在需要使用的页面/模板里,先获取订单/购物车对象,再把字段转成变量:

// 场景1:结账页(用购物车对象)
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach ( $cart_items as $item ) {
    $participant_name = isset( $item['course_participant_name'] ) ? $item['course_participant_name'] : '';
    $session_time = isset( $item['course_session_time'] ) ? $item['course_session_time'] : '';
    // 其他字段同理
}

// 场景2:订单详情/邮件(用订单对象)
$order = wc_get_order( $order_id ); // 如果已有$order对象可直接使用
$participant_name = $order->get_meta( 'course_participant_name', true );
$participant_age = $order->get_meta( 'course_participant_age', true );

分场景实现精准自定义

一、结账页自定义输出

用钩子在结账页指定位置输出变量,完全控制排版:

add_action( 'woocommerce_checkout_after_order_review', 'custom_checkout_course_fields' );
function custom_checkout_course_fields() {
    global $woocommerce;
    $cart_items = $woocommerce->cart->get_cart();

    echo '<div class="course-registration-info">';
    echo '<h3>课程注册信息</h3>';
    
    foreach ( $cart_items as $item ) {
        $product = wc_get_product( $item['product_id'] );
        echo '<p><strong>课程名称:</strong>' . $product->get_name() . '</p>';
        
        $participant_name = isset( $item['course_participant_name'] ) ? $item['course_participant_name'] : '';
        $session_time = isset( $item['course_session_time'] ) ? $item['course_session_time'] : '';
        
        if ( $participant_name ) {
            echo '<p><strong>参与者姓名:</strong>' . esc_html( $participant_name ) . '</p>';
        }
        if ( $session_time ) {
            echo '<p><strong>课程时段:</strong>' . esc_html( $session_time ) . '</p>';
        }
    }
    echo '</div>';
}

二、订单详情页(前台+后台)

前台用户订单详情

用钩子在订单表格后添加自定义区块:

add_action( 'woocommerce_order_details_after_order_table', 'custom_order_course_fields' );
function custom_order_course_fields( $order ) {
    $participant_name = $order->get_meta( 'course_participant_name', true );
    $participant_age = $order->get_meta( 'course_participant_age', true );
    $session_time = $order->get_meta( 'course_session_time', true );

    if ( $participant_name || $participant_age || $session_time ) {
        echo '<h2>课程注册详情</h2>';
        echo '<table class="woocommerce-table course-details-table">';
        if ( $participant_name ) {
            echo '<tr><th>参与者姓名</th><td>' . esc_html( $participant_name ) . '</td></tr>';
        }
        if ( $participant_age ) {
            echo '<tr><th>参与者年龄</th><td>' . esc_html( $participant_age ) . '</td></tr>';
        }
        if ( $session_time ) {
            echo '<tr><th>课程时段</th><td>' . esc_html( $session_time ) . '</td></tr>';
        }
        echo '</table>';
    }
}

后台管理员订单详情

在订单编辑页添加自定义字段区块:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_admin_order_course_fields' );
function custom_admin_order_course_fields( $order ) {
    $participant_name = $order->get_meta( 'course_participant_name', true );
    if ( $participant_name ) {
        echo '<div class="course-admin-field">';
        echo '<h4>课程注册信息</h4>';
        echo '<p><strong>参与者姓名:</strong>' . esc_html( $participant_name ) . '</p>';
        echo '</div>';
    }
}

三、客户邮件模板自定义

通过模板重载来修改邮件内容(避免插件更新覆盖):

  1. 复制WooCommerce默认邮件模板(比如wp-content/plugins/woocommerce/templates/emails/customer-completed-order.php)到你的主题文件夹下的woocommerce/emails/目录
  2. 在模板合适位置添加变量输出代码:
// 比如在订单详情表格后添加
<h2><?php esc_html_e( '课程注册信息', 'woocommerce' ); ?></h2>
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%;" border="1">
    <?php 
    $participant_name = $order->get_meta( 'course_participant_name', true );
    $session_time = $order->get_meta( 'course_session_time', true );
    ?>
    <?php if ( $participant_name ) : ?>
    <tr>
        <th class="td" scope="row"><?php esc_html_e( '参与者姓名', 'woocommerce' ); ?></th>
        <td class="td"><?php echo esc_html( $participant_name ); ?></td>
    </tr>
    <?php endif; ?>
    <?php if ( $session_time ) : ?>
    <tr>
        <th class="td" scope="row"><?php esc_html_e( '课程时段', 'woocommerce' ); ?></th>
        <td class="td"><?php echo esc_html( $session_time ); ?></td>
    </tr>
    <?php endif; ?>
</table>

这种方式完全满足你“严格控制输出内容”的需求,每个字段都能单独控制是否显示、显示位置和样式,不会出现批量输出元数据的冗余内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:29:48