简体   繁体   中英

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?

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

  1. Get the $attributeId
  2. Get the Attribute group Id of the attribute set (eg 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 
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM