How to Create new Group and Attribute in Magento

magento

We will create a Group with the Name as “Attribute Group” and we will create 3 Attributes with the name as “Attribute1” , “Attribute2” amd “Attribute3”
We will Assign All the 3 Attributes to the new created Attribute Group.
But as we will write all the code into the sql script so we will Remove all the Attribute first so if its already created then it will be removed from
below code and then re created again and then assigned to new Group
Attribute 1 will be Drop down and other two will be text boxes
Please check below to code:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to Create Group and Attribute</title>
</head>
<body>
<!-- Create New Group and add attribute and Assign Attribute to group-->
<?php
    // Add new Attribute group
    $groupName = 'AttributeGroup';
    $attributeCode1 = 'attribute1';
    $attributeName1 = 'Attribute 1';

    $attributeCode2 = 'attribute2';
    $attributeName2 = 'Attribute 2';

    $attributeCode3 = 'attribute3';
    $attributeName3 = 'Attribute 3';


    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $setup->removeAttribute('catalog_product', $attributeCode1);
    $setup->removeAttribute('catalog_product', $attributeCode2);
    $setup->removeAttribute('catalog_product', $attributeCode3);


    $entityTypeId = $setup->getEntityTypeId('catalog_product');
    $setup->addAttributeGroup('catalog_product', 'Default', $groupName, 1000);

    $setup->addAttribute('catalog_product', $attributeCode1, array(
        'type'       => 'int',
        'input'      => 'select',
        'label'      => $attributeName1,
        'sort_order' => 1000,
        'position' => 10,
        'required'   => false,
        'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'backend'    => 'eav/entity_attribute_backend_array',
        'option'     => array(
            'values' => array(
                0 => 'Option 1',
                1 => 'Option 2',
            )
        ),

    ));

    $setup->addAttribute('catalog_product', $attributeCode2, array(
            'label'    => $attributeName2,
            'group'    => $groupName,
            'type'     => 'int',
            'input'    => 'text',
            'visible'  => true,
            'required' => false,
            'position' => 11,
            'sort_order' => 1001,
            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        ));
    $setup->addAttribute('catalog_product', $attributeCode3, array(
            'label'    => $attributeName3,
            'group'    => $groupName,
            'type'     => 'int',
            'input'    => 'text',
            'visible'  => true,
            'required' => false,
            'position' => 12,
            'sort_order' => 1002,
            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        ));
        
        
    $model = Mage::getResourceModel('catalog/setup','catalog_setup');
    $model->addAttributeToSet('catalog_product', 'Default', $groupName, $attributeCode1); 
    $model->addAttributeToSet('catalog_product', 'Default', $groupName, $attributeCode2); 
    $model->addAttributeToSet('catalog_product', 'Default', $groupName, $attributeCode3); 
    $model->addAttributeToSet('catalog_product', 'Default', $groupName, 'AttributeGroup'); 
    $installer->endSetup();
    <!---------------- Ends Here------------> 
?>
Posted in Uncategorized