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

WooCommerce后台手动添加订单时动态添加产品自定义字段

WooCommerce后台手动添加订单时产品动态自定义字段实现(子主题版)

1. 在订单编辑页面添加动态自定义字段

将以下代码添加到子主题的functions.php文件中,实现根据所选产品动态生成对应字段:

/**
 * 在后台订单编辑页面的订单项下方添加动态自定义字段
 */
add_action( 'woocommerce_after_order_itemmeta', 'add_dynamic_order_item_fields', 10, 3 );
function add_dynamic_order_item_fields( $item_id, $item, $product ) {
    // 仅针对后台订单编辑页面,且仅处理产品类型订单项
    if ( ! is_admin() || $item->get_type() !== 'line_item' ) return;

    // 根据产品ID定义对应字段,可自行扩展
    $product_fields = array(
        123 => array( // 产品ID=123
            array(
                'label' => '定制尺寸',
                'name'  => 'custom_size',
                'type'  => 'text'
            )
        ),
        456 => array( // 产品ID=456
            array(
                'label' => '雕刻内容',
                'name'  => 'engraving_text',
                'type'  => 'textarea'
            ),
            array(
                'label' => '颜色选择',
                'name'  => 'color_option',
                'type'  => 'select',
                'options' => array(
                    'red' => '红色',
                    'blue' => '蓝色',
                    'black' => '黑色'
                )
            )
        )
    );

    // 如果当前产品有对应字段,输出字段HTML
    if ( isset( $product_fields[$product->get_id()] ) ) {
        echo '<div class="custom-order-item-fields" style="margin: 10px 0; padding: 10px; background: #f9f9f9;">';
        foreach ( $product_fields[$product->get_id()] as $field ) {
            // 获取已保存的字段值
            $value = wc_get_order_item_meta( $item_id, $field['name'], true );
            
            echo '<p style="margin: 5px 0;">';
            echo '<label style="display: inline-block; width: 120px; font-weight: bold;">' . esc_html( $field['label'] ) . ':</label>';
            
            switch ( $field['type'] ) {
                case 'text':
                    echo '<input type="text" name="custom_order_item[' . $item_id . '][' . $field['name'] . ']" value="' . esc_attr( $value ) . '" style="width: 250px;">';
                    break;
                case 'textarea':
                    echo '<textarea name="custom_order_item[' . $item_id . '][' . $field['name'] . ']" style="width: 250px; height: 80px;">' . esc_textarea( $value ) . '</textarea>';
                    break;
                case 'select':
                    echo '<select name="custom_order_item[' . $item_id . '][' . $field['name'] . ']" style="width: 250px;">';
                    foreach ( $field['options'] as $opt_value => $opt_label ) {
                        echo '<option value="' . esc_attr( $opt_value ) . '" ' . selected( $value, $opt_value, false ) . '>' . esc_html( $opt_label ) . '</option>';
                    }
                    echo '</select>';
                    break;
            }
            echo '</p>';
        }
        echo '</div>';
    }
}

2. 保存自定义字段值

添加以下代码,实现字段值的保存:

/**
 * 保存订单订单项的自定义字段值
 */
add_action( 'woocommerce_save_order_items', 'save_dynamic_order_item_fields', 10, 2 );
function save_dynamic_order_item_fields( $order_id, $order ) {
    // 检查是否有自定义字段数据提交
    if ( isset( $_POST['custom_order_item'] ) && is_array( $_POST['custom_order_item'] ) ) {
        foreach ( $_POST['custom_order_item'] as $item_id => $fields ) {
            foreach ( $fields as $field_name => $field_value ) {
                // 更新或添加订单项元数据
                wc_update_order_item_meta( $item_id, $field_name, sanitize_text_field( $field_value ) );
            }
        }
    }
}

3. 前后台订单页面显示自定义字段

添加以下代码,让自定义字段在前台客户订单页面和后台管理员订单页面都能显示:

/**
 * 在前后台订单页面显示自定义字段
 */
add_action( 'woocommerce_order_item_meta_end', 'display_dynamic_order_item_fields', 10, 4 );
function display_dynamic_order_item_fields( $item_id, $item, $order, $plain_text ) {
    // 仅处理产品类型订单项
    if ( $item->get_type() !== 'line_item' ) return;

    // 定义需要显示的字段标签映射(和之前的字段name对应)
    $field_labels = array(
        'custom_size' => '定制尺寸',
        'engraving_text' => '雕刻内容',
        'color_option' => '颜色选择'
    );

    foreach ( $field_labels as $field_name => $field_label ) {
        $value = wc_get_order_item_meta( $item_id, $field_name, true );
        if ( ! empty( $value ) ) {
            // 如果是选择框类型,转换为中文标签(可选)
            if ( $field_name === 'color_option' ) {
                $value_map = array(
                    'red' => '红色',
                    'blue' => '蓝色',
                    'black' => '黑色'
                );
                $value = isset( $value_map[$value] ) ? $value_map[$value] : $value;
            }
            
            if ( $plain_text ) {
                echo PHP_EOL . esc_html( $field_label . ': ' . $value );
            } else {
                echo '<br><strong>' . esc_html( $field_label ) . ':</strong> ' . esc_html( $value );
            }
        }
    }
}

自定义调整说明

  • 修改$product_fields数组,可根据不同产品ID添加对应的字段类型(文本框、多行文本、下拉框等)
  • $field_labels数组要和字段的name对应,确保显示正确的标签
  • 如需更多字段类型(比如复选框),可在add_dynamic_order_item_fields函数中扩展switch分支

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 04:46:26