View Categories

How to make address fields read only

1 min read

Use the following snippets to make the billing and/or shipping address fields read-only.

Make billing fields read only: #

PHP
function sl_cc_make_billing_fields_readonly($fields){
  $fields['billing_address_1']['custom_attributes'] = array('readonly'=>'readonly');
  // Below is an example of making the billing city field readonly.
  // $fields['billing_city']['custom_attributes'] = array('readonly'=>'readonly');
  return $fields;
}
add_filter('woocommerce_billing_fields', 'sl_cc_make_billing_fields_readonly', PHP_INT_MAX);

Make shipping fields read only: #

PHP
function sl_cc_make_shipping_fields_readonly($fields){
  $fields['shipping_address_1']['custom_attributes'] = array('readonly'=>'readonly');
  // Below is an example of making the shipping city field readonly.
  // $fields['shipping_city']['custom_attributes'] = array('readonly'=>'readonly');
  return $fields;
}
add_filter('woocommerce_shipping_fields', 'sl_cc_make_shipping_fields_readonly', PHP_INT_MAX);

You may add both of these snippets to the same field if you wish to disable both fields.


It’s possible to make other fields on the page readonly, in the snippets above I commented out an example showing how to disable the billing and shipping city fields. You can duplicate the line where the ...address_1 fields are being set as read only and simply change the field to one of the following to have them become disabled:

Billing

billing_first_name

billing_last_name

billing_company

billing_address_1

billing_address_2

billing_city

billing_postcode

billing_country

billing_state

billing_email

billing_phone

Shipping

shipping_first_name

shipping_last_name

shipping_company

shipping_address_1

shipping_address_2

shipping_city

shipping_postcode

shipping_country

shipping_state