Skip to content

Instantly share code, notes, and snippets.

@toh82
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toh82/ef1f7cfd0188427281f1 to your computer and use it in GitHub Desktop.
Save toh82/ef1f7cfd0188427281f1 to your computer and use it in GitHub Desktop.
Magento EE Customer Attributes Install-script
<?xml version="1.0"?>
<config>
<modules>
<Project_Customer>
<version>0.1.0</version>
</Project_Customer>
</modules>
<global>
<helpers>
<project_customer>
<class>Project_Customer_Helper</class>
</project_customer>
</helpers>
<resources>
<project_customer_setup>
<setup>
<module>Project_Customer</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</project_customer_setup>
<project_customer_setup>
<connection>
<use>core_write</use>
</connection>
</project_customer_setup>
<project_customer_setup>
<connection>
<use>core_read</use>
</connection>
</project_customer_setup>
</resources>
</global>
</config>
<?php
/* @var Mage_Eav_Model_Entity_Setup $oInstaller */
$oInstaller = $this;
/** @var project_Customer_Helper_Install $oAttributeInstallHelper */
$oAttributeInstallHelper = Mage::helper('project_customer/install');
$oInstaller->startSetup();
// adding some new attributes
$aNewCustomerAttributeData = array(
'new_customer_field' => array(
'entity' => 'customer',
'data' => array(
'type' => 'varchar',
'backend' => '',
'label' => 'Function',
'input' => 'text',
'global' => 1,
'source' => '',
'visible' => true,
'required' => true,
'default' => '',
'frontend' => '',
'unique' => false,
'note' => 'Custom Attribute'
),
'advanced' => array(
'is_used_for_customer_segment' => true,
'is_system' => 0,
'is_user_defined' => 1,
'is_visible' => 1,
'sort_order' => 100,
'used_in_forms' => array(
'checkout_register',
'customer_account_create',
'customer_account_edit',
'adminhtml_customer',
'adminhtml_checkout'
)
)
),
);
foreach ($aNewCustomerAttributeData as $sIdentifier => $aData) {
$oInstaller->addAttribute($aData['entity'], $sIdentifier, $aData['data']);
$oAttributeInstallHelper->addNewAttributeToGroup($aData['entity'], $sIdentifier, $aData['advanced']['sort_order']);
$oAttributeInstallHelper->saveCustomerAttributeData($sIdentifier, $aData['advanced'], $aData['entity']);
}
// Update some existing attributes
$aUpdateCustomerAttributeData = array(
'taxvat' => array(
'entity' => 'customer',
'data' => array(
'is_visible' => true,
'is_required' => true,
)
),
'company' => array(
'entity' => 'customer_address',
'data' => array(
'is_required' => true
)
)
);
foreach ($aUpdateCustomerAttributeData as $sIdentifier => $aData) {
$oAttributeInstallHelper->saveCustomerAttributeData($sIdentifier, $aData['data'], $aData['entity']);
}
$oInstaller->endSetup();
<?php
class Project_Customer_Helper_Install extends Mage_Core_Helper_Abstract
{
/**
* @param string $sEntityIdentifier
* @param string $sAttributeIdentifier
* @param string $sSortOrder
* @throws Mage_Core_Exception
*/
public function addNewAttributeToGroup ($sEntityIdentifier, $sAttributeIdentifier, $sSortOrder)
{
/** @var Mage_Eav_Model_Entity_Setup $oSetup */
$oSetup = new Mage_Eav_Model_Entity_Setup('core_setup');
$iEntityTypeId = $oSetup->getEntityTypeId($sEntityIdentifier);
$iAttributeSetId = $oSetup->getDefaultAttributeSetId($iEntityTypeId);
$iAttributeGroupId = $oSetup->getDefaultAttributeGroupId($iEntityTypeId, $iAttributeSetId);
$oSetup->addAttributeToGroup(
$iEntityTypeId,
$iAttributeSetId,
$iAttributeGroupId,
$sAttributeIdentifier,
$sSortOrder
);
}
/**
* @param string $sAttributeIdentifier
* @param array<string|array<string>> $aAdvancedAttributeData
* @param string $sEntityIdentifier
*/
public function saveCustomerAttributeData ($sAttributeIdentifier, $aAdvancedAttributeData, $sEntityIdentifier)
{
$oAttribute = Mage::getSingleton("eav/config")->getAttribute($sEntityIdentifier, $sAttributeIdentifier);
$oAttribute->addData($aAdvancedAttributeData);
$oAttribute->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment