Wednesday 21 January 2015

Create the Custom Attribute to Customer

This Post is used for Create the custom drop down attribute to customer and these following files are placed in existing module.
Required files are:
1.Namespace/module/sql/install-1.1.10.php
2.Namespace/module/Model/Attribute/Source/warehouse.php

1.Namespace/module/sql/install-1.1.10.php

<?php
//$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer = $this;
$installer->startSetup();

$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$installer->addAttribute('customer', 'warehouse_name', array(
        'label' => 'Warehouse Name',
        'input' => 'select',
        'type'  => 'varchar',
        'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
        'required' => 1,
        'user_defined' => 1,
        'source' => 'namespace_module/attribute_source_warehouse',
       // 'source' => NULL,
));

$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, 'warehouse_name', 0);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'warehouse_name');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();

$installer->endSetup();
?>

2.Namespace/module/Model/Attribute/Source/warehouse.php

<?php
class Namespace_module_Model_Attribute_Source_Warehouse extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;

    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();

            $this->_options[] = array('label'=> 'Option 1', value=>1);
            $this->_options[] = array('label'=> 'Option 2', value=>3);
            $this->_options[] = array('label'=> 'Option 3', value=>3);
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}
?>

No comments:

Post a Comment

Product Collection with out of stock and In Stock in Magento 2

Product collection with out of stock and In stock. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; $collection = $th...