Adding an Existing Attribute to All Attribute Sets

I have an existing attribute for the embed code. I need to associate this attribute with 120+ existing attribute sets.

If I know the attribute set identifier, how can I go on to add the attribute to all attribute sets programmatically?

+7
source share
5 answers

I was interested in writing code for this problem, so here is a solution that works :)

Run this code in a php script, including mage.php, and let me know if it works well.

replace (firstname) with the attribute code that you want to bulk add to all attribute sets

$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity ( there is different entities in magento ex : catalog_category, order,invoice... etc ) $attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection') ->setCodeFilter('firstname') ->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)->setOrder('attribute_group_id',"ASC")->getFirstItem(); $groupId = $group->getId(); $newItem = Mage::getModel('eav/entity_attribute'); $newItem->setEntityTypeId($attSet->getId()) // catalog_product eav_entity_type id ( usually 10 ) ->setAttributeSetId($setId) // Attribute Set ID ->setAttributeGroupId($groupId) // Attribute Group ID ( usually general or whatever based on the query i automate to get the first attribute group in each attribute set ) ->setAttributeId($attId) // Attribute ID that need to be added manually ->setSortOrder(10) // Sort Order for the attribute in the tab form edit ->save() ; echo "Attribute ".$attCode." Added to Attribute Set ".$set->getAttributeSetName()." in Attribute Group ".$group->getAttributeGroupName()."<br>\n"; } 
+21
source

for people having problems with the code above,

like: calling the getModelInstance () member function for a non-object

you need to add the following to the top of the file:

 include 'app/Mage.php'; Mage::app(); 

edit:

im using magento 1.8.1.0 and the code still doesn't work

I had to add the following line to $ newItem, so the check passes

  ->setAttributeCode($attCode) 
+2
source

The function Mage_Catalog_Model_Resource_Setup::addAttribute() can be used to update, as well as to add attributes. Another useful thing - if you specify a group with this function, it is automatically assigned to all sets.

 $attributeCode = 'name'; // choose your attribute here $setup = Mage::getResourceSingleton('catalog/setup'); $setup->addAttribute('catalog_product', $attributeCode, array( // no need to specify fields already used like 'label' or 'type' 'group' => 'General', 'sort_order' => 10 )); 
+2
source

If you use an external script (and not a magento script setting), this work me with me

 <?php /// run in magento root require_once 'app/Mage.php'; ini_set('display_errors', 1); error_reporting(E_ALL); Mage::app(); $attributeCode = 'my_attr_code'; $group = 'MyGroup'; $sortOrder = 10; //this way you config the setup connections $setup = new Mage_Catalog_Model_Resource_Setup('catalog_setup'); $setup->startSetup(); $setup->addAttribute('catalog_product', $attributeCode, array( 'group' => $group, 'sort_order' => $sortOrder )); 

(as a response to @Eric)

+1
source

Do not use

 Mage::getResourceSingleton('catalog/setup'); 

But use

 Mage::getResourceModel('catalog/setup', 'catalog_setup'); 
0
source

All Articles