Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active May 15, 2017 13:46
Show Gist options
  • Select an option

  • Save evgv/d1a96fe8d5530f0c15d376e12e4dc0f1 to your computer and use it in GitHub Desktop.

Select an option

Save evgv/d1a96fe8d5530f0c15d376e12e4dc0f1 to your computer and use it in GitHub Desktop.
Magento. Add customer attribute.

Add customer attribute

Resources in config.xml

        
<resources>
    <{{MODEL_ALIAS}}_setup>
        <setup>
            <module>{{Vendor_Extension}}</module>
            <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </{{MODEL_ALIAS}}_setup>
    <{{MODEL_ALIAS}}_write>
        <connection>
            <use>core_write</use>
        </connection>
    </{{MODEL_ALIAS}}_write>
    <{{MODEL_ALIAS}}_read>
        <connection>
            <use>core_read</use>
        </connection>
    </{{MODEL_ALIAS}}_read>
</resources>
        

Variable $installer must be instance of Mage_Customer_Model_Entity_Setup

Install script

<?php

$installer = $this; // or new Mage_Customer_Model_Entity_Setup('core_setup'); if you dosen't set this class in resources

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup("core_setup");

$entityTypeId     = $setup->getEntityTypeId("customer");
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$attributeCode  = "telephone"; // attribute code what will be created

$installer->removeAttribute('customer', $attributeCode); // remove attribute if exist
$installer->addAttribute(
        "customer", 
        $attributeCode,  
        array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Telephone",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default"  => "",
            "frontend" => "",
            "unique"   => false,
            "note"     => "Customer telephone"
        )
    );

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCode);

$setup->addAttributeToGroup(
            $entityTypeId,
            $attributeSetId,
            $attributeGroupId,
            $attributeCode,
            "100"  // sort_order
        );

$usedInForms = array(
                    "adminhtml_customer", 
//                    "checkout_register", 
//                    "customer_account_create", 
                    "customer_account_edit", 
//                    "adminhtml_checkout"
                );

$attribute->setData("used_in_forms", $usedInForms)
          ->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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment