/app/code/local/Your/Customattribute/sql/your_customattribute_setup/install-0.1.0.php
<?php
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttribute("customer", "customattribute", array(
"type" => "varchar",
"backend" => "",
"label" => "Custom Attribute",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Custom Attribute"
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "customattribute");
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'customattribute',
'999' //sort_order
);
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->endSetup();
/app/code/local/Your/Customattribute/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Your_Customattribute>
<version>0.1.0</version>
</Your_Customattribute>
</modules>
<global>
<resources>
<Your_Customattribute_setup>
<setup>
<module>Your_Customattribute</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</Your_Customattribute_setup>
<Your_Customattribute_write>
<connection>
<use>core_write</use>
</connection>
</Your_Customattribute_write>
<Your_Customattribute_read>
<connection>
<use>core_read</use>
</connection>
</Your_Customattribute_read>
</resources>
</global>
</config>
app/etc/modules/Your_Customattribute.xml
<?xml version="1.0"?>
<config>
<modules>
<Your_Customattribute>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Your_Customattribute>
</modules>
</config>
Then to retrieve or edit you use:
$customer = Mage::getModel('customer/customer')->load($custid);
$customer->getCustomattribute();
$customer->setCustomattribute($yourjson);
You will have to create event observers for the login event, answered here: How can i get customer data from observer after successful login?
and also likely observers for the customer_save_after in case they alter their address in account mgmt, and one for the quote, which could be in different places depending on what you are going for.
Custom customer attributes will never just 'appear' on the frontend like they do in the backend. The code that displays them on the frontend is located in a custom phtml file.
Magento EE has this functionality built in. I'm not suggesting you NEED to spend that money, I'm just saying it has it. If you want to go ahead and attempt to add custom attributes, it's somewhat complex.
First of all, you must do all of this in a module or it just isn't going to work right, and it's going to be hard to debug/upgrade later.
You have to do these things:
- Create the attribute (you've done this if it shows up in admin)
- override the frontend layout for referenceContainer form.additional.info
- add a template phtml file to show the additional attribute(s)
- add a block PHP file to load the new attributes and create the HTML
- other things like learn how to automate the process and load multiples instead of hard coding it to load just the name of the one you created.
You can load your custom attributes in the Block PHP. Then just add your layout to the customer_account_create.xml
like this:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<update handle="customer_form_template_handle"/>
<body>
<referenceContainer name="form.additional.info">
<block class="Company\Customformattributes\Block\FormCustomer" template="Company_Customformattributes::customattributes.phtml" name="customer_form_user_attributes" cacheable="false">
<action method="setFormCode">
<argument name="code" xsi:type="string">customer_account_edit</argument>
</action>
<action method="setEntityModelClass">
<argument name="code" xsi:type="string">Magento\Customer\Model\Customer</argument>
</action>
</block>
</referenceContainer>
</body>
</page>
This is the magic sauce to get your block PHP to load, to get your phtml to load, and to get it into the correct page.
This isn't a full answer honestly, there is a lot more to it, but you get the basic idea.
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: