How to create a custom product dropdown / select attribute and it's custom Options in Magento



1. Create a setup file in your module's sql folder. For example : mysql4-install-1.0.0 or mysql4-upgrade-1.0.0-1.0.1 if you are upgrading sql script.

2. Add a new dropdown / select product attribute and it's custom options as follows


    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    //Create Attribute
    $installer->addAttribute('catalog_product', 'backorder_time', array(
    'group'             => 'General',//Group name to add this attribute
    'type'              => 'text',
    'backend'           => '',
    'frontend'          => '',
    'label'             => 'Back-Order Duration',
    'input'             => 'select',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'used_in_product_listing' => true,
    'sort_order'        => 500,
    ));
    //Add custom Back-order duration options
    $initialOptions = array('1 - 2 days','1 day','14 - 16 days','2 - 4 days','3 - 5 days','5 - 8 days','7 - 14 days','to 28 days');
    $entityTypeId     = $installer->getEntityTypeId('catalog_product');
    $code ='backorder_time';
    $attributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'backorder_time')->getId();
    foreach($initialOptions as $k=>$v){
$installer->addAttributeOption(array(
'attribute_id' => $attributeId,
'order' => array($k),
'value' => array(array($v)
)
));
    }
    $installer->endSetup();




3. You can also remove the created above product attribute from remove codes as follows

 
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

    //Remove code
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->removeAttribute( 'catalog_product', 'backorder_time' );
 


How to create a custom product dropdown / select attribute and it's custom Options in Magento How to create a custom product dropdown / select attribute and it's custom Options in Magento Reviewed by Web Technology Funda on 11:36:00 PM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!