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

WooCommerce:将客户姓名及账单地址字段设置为只读

实现WooCommerce结账页账单姓名+地址只读功能

你只需要在原有代码的判断条件中,加入账单姓名对应的字段键billing_first_name和billing_last_name即可,修改后的完整代码如下:

add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    foreach ( $checkout_fields['billing'] as $key => $field ){
        // 新增账单姓名字段的判断
        if($key == 'billing_first_name' || $key == 'billing_last_name' || $key == 'billing_address_1' || $key == 'billing_address_2'){
            $key_value = get_user_meta($user_id, $key, true);
            if( strlen($key_value)>0){
                $checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
            }
        }
    }
    return $checkout_fields;
}

代码说明

  • 核心修改是在if条件中追加了billing_first_name和billing_last_name两个字段键,让这两个字段也遵循相同的只读规则:当用户后台已经保存了对应的值时,结账页的该字段就会被设置为只读状态。
  • 保持原有逻辑不变:只有当用户元数据中存在对应字段的有效值时,才会添加只读属性,避免新用户首次结账时无法填写信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 05:54:38