Marty Wallace
Marty Wallace

Reputation: 35744

Magento - add new catalog_product attribute

I want to add a new product attribute and am using code similar to this:

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

$attribute = array(
    'label'             => 'New Attribute',
    'required'          => false,
    'type'              => 'varchar',
    'input'             => 'text'
);
$installer->addAttribute('catalog_product', 'new_attribute', $attribute);

Which works fine.

But, I want to add this attribute to a specific attribute set and ONLY to this specific attribute set.

How can this be achieved?

Upvotes: 0

Views: 193

Answers (1)

Oleg Ishenko
Oleg Ishenko

Reputation: 2233

Assuming you know the $attributeSetId, the procedure is as follows:

  1. Get the $attributeId
  2. Get the Attribute group Id of the attribute set (e.g. the "General" group)
  3. Add the attribute to the set and group

For example

        $attributeId = $installer->getAttributeId(
            'catalog_product', 
            'new_attribute'
        );

        $attributeGroupId = $installer->getAttributeGroup(
            'catalog_product',
            $attributeSetId,
            'General'
        );

        $installer->addAttributeToSet(
            'catalog_product',
            $attributeSetId, 
            $attributeGroupId,
            $attributeId 
        );

Upvotes: 1

Related Questions