Custom Attributes – Add Custom Attributes to Custom Attribute Set Programmatically

attribute-setattributescustomcustom-attributesproduct-attribute

Hi Could someone help me with this?

I created a custom attribute set and custom attribute as

$installer = $this;
/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();

//Create Attribute set with Based on Default attribute set
//$installer->removeAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attr Set');
/*
$skeletonID=$installer->getAttributeSetId('catalog_product','Default');
$entityTypeId = Mage::getModel('catalog/product')
->getResource()
->getEntityType()
->getId(); //product entity type

$attributeSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId($entityTypeId)
->setAttributeSetName("New Attr Set");

$attributeSet->validate();
$attributeSet->save();

$attributeSet->initFromSkeleton($skeletonID)->save();


//Create attribute new_attr
//$installer->removeAttribute('catalog_product', 'new_attr');
$data= array (
'attribute_set' =>  'New Attr Set',
'group' => 'General',
'label'    => 'New Attr',
'visible'     => true,
'type'     => 'int', // multiselect uses comma-sep storage
'input'    => 'boolean',
'system'   => false,
'required' => false,
'user_defined' => 1,//defaults to false; if true, define a group
'source' => 'eav/entity_attribute_source_boolean',
'default' => 1,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
 );

 $installer->addAttribute('catalog_product','new_attr',$data);
 */

This code adds the attibute 'new_attr' to the group 'General' and so the custom attribute are displayed in all the attribute sets such as 'Default' also.

I want to add the custom attibute 'new_attr' only to the custom attribute set 'New Attr Set' under the group 'General'. Is that possible?

Best Answer

Yes, this is possible.

First, set these keys in your $data array to following values to avoid adding attribute to all attribute sets:

'user_defined'         => true,
'group'                => ''

Then add attribute to your attribute set:

$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);