Adding attributes to customer entity
This is the code for a basic int
attribute with text
renderer:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'your_attribute_code_here', array(
'input' => 'text',
'type' => 'int',
'label' => 'Some textual description',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
));
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'your_attribute_code_here',
'999' //sort_order
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();
$setup->endSetup();
The unusual step for adding attributes is the setData('used_in_forms')
this seems to be unique to customer attributes. Without it, the field won't get rendered, certainly not in the adminhtml anyway. You can see the valid options for this array in the customer_form_attribute
database table.
In terms of using a select
with predefined options, this is what you need:
$iAttributeId = $installer->getAttributeId($entityTypeId, 'your_attribute_code_here');
$aClasses = array('TV','DVD','Home Theatre','Air Conditioner','Stereo/Hifi','Game Console','Camcorder','VCR','Set Top Box','PVR');
$aOption = array();
$aOption['attribute_id'] = $iAttributeId;
for($iCount=0;$iCount<sizeof($aClasses);$iCount++){
$aOption['value']['option'.$iCount][0] = $aClasses[$iCount];
}
$setup->addAttributeOption($aOption);
And here is a walk-through on using a custom source for your drop-down
Hope this helps,
JD
Magento - Add attribute to customer entity
I edited your script and it worked fine for me, I can see new attribute in backed now.
Replace this:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
With this:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();
What is the best approach for users to add custom attributes to entities?
I favor adding fields to a table via DDL, but that table should be separate from the main table. That way, you can script changes to your database schema without affecting your users' custom field additions. A right-join is easy enough to accomplish, and you won't need the record in the separate table if there are no custom fields.
If you just want to display data in a vertical fashion, EAV tables can be a good choice. You can also run a pivot query to display them horizontally.
magento2 adding customer attributes
I had similar issue recently, try to add this in 'used_in_forms'.
You might have to remove the attribute and reinstall it:
'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']
edit
Oh I think this should solve the issue, just checked my installData and upgradeData scripts and they all have system => 0. Just add it in.
$customerSetup->addAttribute('customer', 'customershipping_enabled', [
'label'=>'Customer Shipping Enabled',
'type' => 'int',
'input' => 'select',
'source' => Boolean::class,
'required'=>true,
'visible'=>true,
'default' => 0,
'position' => 198,
'system' => 0
]);
It'll be related to this issue:
https://apiworks.net/magento2/magento-2-is-not-saving-the-customer-attribute/
The function getCustomAttributesMetadata is looping through all EAV
attributes and checking if the attribute is marked as “is_system”
inside the “customer_eav_attribute” table, which was the case with my
custom attribute.Solution:
By default, Magento flagged my custom attribute as is_system = 1, so I
just needed to add ‘system’ => false in my upgrade script and execute
it again (after I removed the original attribute directly from the
database. ).
How do you model custom attributes of entities?
The grouping is not going to be easy because what aggregate operator are you going to use on "color"? Note that it is not possible to use your requirement 4 on case 2.
In any case, the aggregating is only difficult because of the variation in data types and can be mitigated by approaching it in a more typesafe way - knowing that it never makes sense to add apples and oranges.
This is the classic EAV model and it has a place in databases where carefully designed. In order to make it a bit more typesafe, I've seen cases where the values are stored in type-safe tables instead of in a single free form varchar column.
Instead of Values:
EntityID int
,AttributeID int
,Value varchar(255)
You have multiple tables:
EntityID int
,AttributeID int
,ValueMoney money
EntityID int
,AttributeID int
,ValueInt int
etc.
Then to get your iPod capacity per generation:
SELECT vG.ValueVarChar AS Generation, SUM(vC.ValueDecimal) AS TotalCapacity
FROM Products AS p
INNER JOIN Attributes AS aG
ON aG.AttributeName = 'generation'
INNER JOIN ValueVarChar AS vG
ON vG.EntityID = p.ProductID
AND vG.AttributeID = aG.AttributeID
INNER JOIN Attributes AS aC
ON aC.AttributeName = 'capacity'
INNER JOIN ValueDecimal AS vC
ON vC.EntityID = p.ProductID
AND vC.AttributeID = aC.AttributeID
GROUP BY vG.ValueVarChar
How do you add custom attributes to Customer Groups in Magento?
If you take a look at the sql installer/update scripts you will find something like this:
$table = $installer->getConnection()
->newTable($installer->getTable('customer/customer_group'))
->addColumn('customer_group_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Customer Group Id')
->addColumn('customer_group_code', Varien_Db_Ddl_Table::TYPE_TEXT, 32, array(
'nullable' => false,
), 'Customer Group Code')
->addColumn('tax_class_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'default' => '0',
), 'Tax Class Id')
->setComment('Customer Group');
As you can see its a simple mysql4 table and you simply need to add a column to the group. It is not EAV so you dont work with attributes on that one!
The new colum will not show up in the form or grid! You have to add this manually via observer or rewriting Mage_Adminhtml_Block_Customer_Group_Edit_Form
or Mage_Adminhtml_Block_Customer_Group_Grid
by adding something like this for e.g text field:
$fieldset->addField('your_column', 'text',
array(
'name' => 'Your_Column',
'label' => Mage::helper('customer')->__('Tax Class'),
'title' => Mage::helper('customer')->__('Tax Class'),
'class' => 'required-entry',
'required' => true
)
);
Related Topics
Php7.1 JSON_Encode() Float Issue
PHP Check Whether Property Exists in Object or Class
Mysql_Real_Escape_String VS Addslashes
How to Check If a Row Exist in the Database Using Pdo
How to Check If String Is a Valid Xml Element Name
Prevent Innodb Auto Increment on Duplicate Key
Doing Http Requests from Laravel to an External API
How to Use Multiple Databases Dynamically for One Model in Cakephp
Ajax File Download Using Jquery, PHP
Get the Sum of All Digits in a Numeric String
What Does This Symbol Mean in PHP <=
When to Use HTMLspecialchars() Function
Sort an Array by a Child Array's Value in PHP
How to Prevent PHP Sessions Being Shared Between Different Apache Vhosts