This little script is an excellent time saver for adding a new attribute to all of the attribute sets configured in your Magento store.
There are a few attributes to update:
- Replace
**ATTRIBUTE CODE**with the code of the attribute you wish to add. - Replace
**ATTRIBUTE GROUP NAME**with the group name - Replace
**SORT ORDER**with sort order you want it to display, as you’d expect.
Upload the script to the Magento root folder and hit with your browser.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem();
$attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection();
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('**ATTRIBUTE CODE**')
->getFirstItem();
$attCode = $attributeInfo->getAttributeCode();
$attId = $attributeInfo->getId();
foreach ($attSetCollection as $a) {
$set = Mage::getModel('eav/entity_attribute_set')->load($a->getId());
$setId = $set->getId();
$group = Mage::getModel('eav/entity_attribute_group')->getCollection()->addFieldToFilter('attribute_set_id',$setId)->addFieldToFilter('attribute_group_name', '**ATTRIBUTE GROUP NAME**')->setOrder('attribute_group_id',ASC)->getFirstItem();
$groupId = $group->getId();
$addAttribute = Mage::getModel('eav/entity_attribute');
$addAttribute->setEntityTypeId($attSet->getId())
->setAttributeSetId($setId)
->setAttributeGroupId($groupId)
->setAttributeId($attId)
->setSortOrder(**SORT ORDER**)
->save();
}