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

如何保存WooCommerce结账页面自定义选择字段数据

WooCommerce自定义结账字段无法保存问题

你已在结账页面添加选择型自定义字段,但该字段未与其他字段一同保存,插件中无法看到该字段。现有代码如下:

//* Add select field to the checkout page
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');

function wps_add_select_checkout_field( $checkout ) {

    woocommerce_form_field( 'studio', array(
        'type'          => 'select',
        'class'         => array( 'wps-drop' ),
        'label'         => __( 'Select your studio' ),
        'required'    => true,
        'priority'    => 5, // Priority sorting option
        'options'       => array(
            'blank'     => __( 'Select Your Studio', 'wps' ),
           'Ukraine & Poland' => __('Ukraine & Poland', 'wps' ),
            'Israel' => __('Israel',  'wps' ),
           'Kyiv' => __('Kyiv',  'wps' ),
            'Finland ' => __('Finland',  'wps' )
        )
 ),
    $checkout->get_value( 'studio' ));
}

//* Process the checkout
 add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
 function wps_select_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if ($_POST['studio'] == "blank")
     wc_add_notice( '<strong>Please select your studio</strong>', 'error' );
 }

只需添加以下代码,即可实现字段的保存与多场景显示:

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

将字段值存入订单数据库:

// 保存自定义字段到订单
add_action('woocommerce_checkout_update_order_meta', 'wps_save_studio_field_to_order');
function wps_save_studio_field_to_order( $order_id ) {
    if ( ! empty( $_POST['studio'] ) ) {
        update_post_meta( $order_id, '_studio', sanitize_text_field( $_POST['studio'] ) );
    }
}

2. 在后台订单编辑页显示字段

让管理员在后台能查看该字段值:

// 在后台订单编辑页显示字段
add_action('woocommerce_admin_order_data_after_billing_address', 'wps_display_studio_field_in_admin_order', 10, 1);
function wps_display_studio_field_in_admin_order( $order ) {
    $studio = get_post_meta( $order->get_id(), '_studio', true );
    if ( $studio ) {
        echo '<p><strong>' . __('Selected Studio') . ':</strong> ' . esc_html( $studio ) . '</p>';
    }
}

3. 在前端订单详情页显示字段

让客户在个人订单页查看选择的studio:

// 在前端订单详情页显示字段
add_action('woocommerce_order_details_after_order_table', 'wps_display_studio_field_in_frontend_order');
function wps_display_studio_field_in_frontend_order( $order ) {
    $studio = get_post_meta( $order->get_id(), '_studio', true );
    if ( $studio ) {
        echo '<p><strong>' . __('Selected Studio') . ':</strong> ' . esc_html( $studio ) . '</p>';
    }
}

4. 在订单邮件中包含字段(可选)

若需要在订单确认邮件里展示该字段,添加以下代码:

// 在订单邮件中显示字段
add_action('woocommerce_email_after_order_table', 'wps_display_studio_field_in_emails', 10, 4);
function wps_display_studio_field_in_emails( $order, $sent_to_admin, $plain_text, $email ) {
    $studio = get_post_meta( $order->get_id(), '_studio', true );
    if ( $studio ) {
        echo '<p><strong>' . __('Selected Studio') . ':</strong> ' . esc_html( $studio ) . '</p>';
    }
}

将上述代码添加到主题的functions.php文件,或使用自定义代码插件(如Code Snippets),自定义字段即可被保存,并在后台订单编辑页、前端订单详情页及订单邮件中显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 14:12:45