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

WooCommerce结账页ACF自定义配送字段同步至订单及邮件

解决WooCommerce自定义ACF配送字段同步订单及邮件问题

我来帮你搞定这个需求——直接在checkout-form.php里加了ACF自定义配送下拉字段后,WooCommerce不会自动处理它的提交和存储,我们需要通过几个钩子来完成数据同步、订单展示和邮件添加的全流程:

1. 捕获并保存字段值到订单元数据

首先要把结账时用户选择的字段值存到订单的元数据里,我们用woocommerce_checkout_update_order_meta钩子来实现。记得替换代码里的字段键为你自己ACF字段的实际键(比如field_xxxxxxx,可以在ACF字段编辑页面找到):

// 保存自定义配送方式到订单元数据
add_action('woocommerce_checkout_update_order_meta', 'save_custom_delivery_method_to_order');
function save_custom_delivery_method_to_order($order_id) {
    // 检查POST参数是否存在,替换成你的字段对应的参数名
    if (!empty($_POST['acf']['field_xxxxxxx'])) {
        // 安全处理数据
        $delivery_method = sanitize_text_field($_POST['acf']['field_xxxxxxx']);
        // 保存到订单元数据,也可以用ACF的update_field函数:update_field('你的字段名', $delivery_method, $order_id)
        update_post_meta($order_id, '_custom_delivery_method', $delivery_method);
    }
}

2. 在订单页面显示字段值

接下来要让用户和管理员在订单详情里看到这个配送方式,分别处理前台订单页和后台订单编辑页:

前台订单详情页显示

// 前台订单详情页添加自定义配送方式展示
add_action('woocommerce_order_details_after_order_table', 'display_custom_delivery_method_on_frontend_order');
function display_custom_delivery_method_on_frontend_order($order) {
    $delivery_method = get_post_meta($order->get_id(), '_custom_delivery_method', true);
    // 如果用ACF的get_field则写成:$delivery_method = get_field('你的字段名', $order->get_id());
    if (!empty($delivery_method)) {
        echo '<tr><th>自定义配送方式:</th><td>' . esc_html($delivery_method) . '</td></tr>';
    }
}

后台订单编辑页显示

// 后台订单编辑页添加自定义配送方式展示
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_delivery_method_in_admin');
function display_custom_delivery_method_in_admin($order) {
    $delivery_method = get_post_meta($order->get_id(), '_custom_delivery_method', true);
    if (!empty($delivery_method)) {
        echo '<div class="address"><p><strong>' . __('自定义配送方式', 'woocommerce') . ':</strong> ' . esc_html($delivery_method) . '</p></div>';
    }
}

3. 把字段值加入订单邮件

最后要让这个配送方式出现在所有订单通知邮件里,用woocommerce_email_order_meta_fields钩子:

// 订单邮件中添加自定义配送方式字段
add_filter('woocommerce_email_order_meta_fields', 'add_custom_delivery_method_to_order_emails', 10, 3);
function add_custom_delivery_method_to_order_emails($fields, $sent_to_admin, $order) {
    $delivery_method = get_post_meta($order->get_id(), '_custom_delivery_method', true);
    if (!empty($delivery_method)) {
        $fields['custom_delivery_method'] = array(
            'label' => __('自定义配送方式', 'woocommerce'),
            'value' => esc_html($delivery_method),
        );
    }
    return $fields;
}

额外注意事项

  • 确保你在checkout-form.php里添加的下拉框name属性正确对应,比如ACF字段的name通常是acf[field_xxxxxxx],如果你的模板里写的是其他name,要同步修改代码里的POST参数。
  • 所有代码都建议加到你当前主题的functions.php文件里,或者用自定义插件来维护,避免主题更新丢失修改。
  • 记得对用户输入的数据做安全处理,代码里的sanitize_text_field和esc_html就是为了防止XSS攻击。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:23:48