We need to use a install script for that and our script should extend the functionality of Mage_Catalog_Model_Resource_Setup class. And we do this in our NameSpace_ModuleName/etc/config.xml, under <global> tag --

<resources>
    <practice_training_setup> <!-- here name space is Practice and module name is training, you can have your own -->
        <setup>
            <module>Practice_Training</module>
            <class>Mage_Catalog_Model_Resource_Setup</class>
        </setup>
    </practice_training_setup>
</resources>

Here’s the code which will create a new product attribute and assign that attribute to our custom new attribute group --

<?php

/** @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$productTypes = array(
    Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
    Mage_Catalog_Model_Product_Type::TYPE_BUNDLE,
    Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
    Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL
);
$productTypes = join(',', $productTypes);

/**
 * Add attributes to the eav/attribute table
 */
$installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'my_attribute_code',
    array(
        'type'                    => 'text',
        'backend'                 => '',
        'frontend'                => '',
        'label'                   => 'My Attribute Label',
        'input'                   => 'text',
        'class'                   => '',
        'source'                  => '',
        'global'                  => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'visible'                 => true,
        'required'                => true,
        'user_defined'            => false,
        'default'                 => '',
        'searchable'              => false,
        'filterable'              => false,
        'comparable'              => false,
        'visible_on_front'        => false,
        'unique'                  => false,
        'apply_to'                => $productTypes, 
        'is_configurable'         => false,
        'used_in_product_listing' => false
    )
);

$getDefaultSetId = $installer->getAttributeSetId('catalog_product', 'default');

$installer->addAttributeGroup(
    'catalog_product',
    $defaultSetId,
    'My New Group'
);

//get the id of our new attribute group
$getGroupId = $installer->getAttributeGroup(
    'catalog_product',
    $defaultSetId,
    'My New Group',
    'attribute_group_id'
);

/* 
$getAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'my_attribute_code')->getId();
*/
$getAttributeId = $installer->getAttributeId(
    'catalog_product',
    'my_attribute_code'
);

//assign the attribtue to default attribute set and to our new attribute group
if ($attributeId > 0) {
    $installer->addAttributeToSet(
        'catalog_product',
        $getDefaultSetId,
        $getGroupId,
        $getAttributeId
    );
}

$installer->endSetup();

Cheers!

Categorized in: