Magento – How to make custom customer attribute read only

attributescheckoutcustomer-attributemagento-1

I have created a custom customer attribute that shows up on the checkout page. I am trying to make this read only. That is show a value in the text field but not have it be editable. I tried adding the following parameter

 'readonly' => true,
  'value' => 'xxxx'

to my addattribute script. I did not get an error when i ran the script and the attribute was added. I see the field in my checkout form and xxxx shows up correctly but it is not read only, I can still edit the field. Does anyone know how to make the text field not editable?

Best Answer

1) If you want it to be read-only in magento forms, you can change its input type to 'label', as mentioned in this SO answer: https://magento.stackexchange.com/a/79465/22620

2) On a side note: If you want to show attribute only in backend, but not in frontends forms, use similar code in your attribute installer:

$attributeCode = 'my_custom_attribute';
$used_in_forms = array("adminhtml_customer"); // Only show in admin panel

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCode);
$attribute
    ->setData("used_in_forms", $used_in_forms)
    ->setData("is_used_for_customer_segment", true)
    ->setData("is_visible", 0) // Hide on frontend
    ;

$attribute->save();