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

如何将WooCommerce结账页所有账单字段设置为只读

实现方案

你原代码中只对billing_address_1、billing_address_2两个特定字段做了只读设置,要覆盖所有账单字段,只需移除特定字段的条件判断,遍历所有账单字段统一处理即可,修改后代码如下:

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;
    // 仅对已登录用户设置只读,避免未登录用户无法填写信息
    if ($user_id) {
        foreach ( $checkout_fields['billing'] as $key => $field ){
            $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( strlen($key_value) > 0 )判断即可
  • 若需要对未登录用户也生效只读规则,删除外层if ($user_id)的判断即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 15:15:07