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

WooCommerce:将自定义字段添加至产品变体名称(购物车及结账页)

实现自定义字段关联产品变体并同步到购物车/结账流程

核心思路

你提到的需求完全可以实现:通过WooCommerce的核心钩子,将用户输入的自定义字段数据与对应变体绑定,同步到购物车项、结账页,最终保存到订单中。下面是分步实现代码:


1. 把自定义字段数据绑定到购物车项

使用woocommerce_add_cart_item_data钩子,将用户提交的字段值存储到购物车项的自定义数据中,同时关联当前选中的变体ID:

add_filter('woocommerce_add_cart_item_data', 'save_custom_field_to_cart_item', 10, 3);
function save_custom_field_to_cart_item($cart_item_data, $product_id, $variation_id) {
    // 仅针对目标产品ID处理
    if ($product_id != 19652) return $cart_item_data;

    // 获取用户提交的自定义字段值
    $first_name = isset($_POST['first_custom_name']) ? sanitize_text_field($_POST['first_custom_name']) : '';
    $second_name = isset($_POST['sec_custom_name']) ? sanitize_text_field($_POST['sec_custom_name']) : '';

    // 根据变体ID匹配对应的字段值(替换成你实际的变体ID)
    $target_name = '';
    if ($variation_id == 'YOUR_FIRST_VARIATION_ID') {
        $target_name = $first_name;
    } elseif ($variation_id == 'YOUR_SECOND_VARIATION_ID') {
        $target_name = $second_name;
    }

    if (!empty($target_name)) {
        // 生成唯一键,避免同产品不同变体的自定义数据冲突
        $cart_item_data['custom_bottle_name'] = array(
            'value' => $target_name,
            'unique_key' => md5(microtime().rand())
        );
    }

    return $cart_item_data;
}

注意:请将YOUR_FIRST_VARIATION_ID和YOUR_SECOND_VARIATION_ID替换为你产品实际的变体ID,这样就能实现不同变体对应不同字段数据的需求。


2. 在购物车和结账页显示自定义字段

通过woocommerce_get_item_data钩子,将存储的自定义数据展示在购物车项和结账页的变体名称下方:

add_filter('woocommerce_get_item_data', 'display_custom_field_in_cart', 10, 2);
function display_custom_field_in_cart($item_data, $cart_item) {
    if (isset($cart_item['custom_bottle_name']['value'])) {
        $item_data[] = array(
            'key' => __('瓶身名称', 'woocommerce'),
            'value' => $cart_item['custom_bottle_name']['value']
        );
    }
    return $item_data;
}

如果需要直接追加到变体名称后面,用woocommerce_cart_item_name钩子:

add_filter('woocommerce_cart_item_name', 'append_custom_field_to_variation_name', 10, 3);
function append_custom_field_to_variation_name($name, $cart_item, $cart_item_key) {
    if (isset($cart_item['custom_bottle_name']['value'])) {
        $name .= '<br><small>' . __('定制名称:', 'woocommerce') . ' ' . esc_html($cart_item['custom_bottle_name']['value']) . '</small>';
    }
    return $name;
}

3. 将自定义字段保存到订单项

使用woocommerce_checkout_create_order_line_item钩子,把购物车中的自定义数据永久保存到订单行项中:

add_action('woocommerce_checkout_create_order_line_item', 'save_custom_field_to_order_item', 10, 4);
function save_custom_field_to_order_item($item, $cart_item_key, $values, $order) {
    if (isset($values['custom_bottle_name']['value'])) {
        $item->add_meta_data(__('瓶身定制名称', 'woocommerce'), $values['custom_bottle_name']['value']);
    }
}

4. 在订单详情和邮件中展示(可选)

如果需要在后台订单详情、用户订单页以及订单邮件中显示定制信息,添加以下代码:

add_action('woocommerce_order_item_meta_end', 'display_custom_field_in_order', 10, 4);
function display_custom_field_in_order($item_id, $item, $order, $plain_text) {
    $custom_name = $item->get_meta(__('瓶身定制名称', 'woocommerce'));
    if (!empty($custom_name)) {
        if ($plain_text) {
            echo PHP_EOL . __('瓶身定制名称:', 'woocommerce') . ' ' . $custom_name;
        } else {
            echo '<br><strong>' . __('瓶身定制名称:', 'woocommerce') . '</strong> ' . $custom_name;
        }
    }
}

关键说明

  • 字段安全:所有用户输入都通过sanitize_text_field和esc_html处理,避免XSS安全问题
  • 变体匹配:通过变体ID精准关联对应的字段值,确保不同变体对应不同的用户输入
  • 数据持久化:从购物车到订单的全流程数据同步,不会丢失定制信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 07:54:51