如何将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
相关产品推荐
相关产品推荐

