Reputation: 1062
I created a new attribute ("for example my_attribute"). Magento's store already has many attribute sets (approx 20). It's a very time consuming process if I manually add the attribute to the sets because my server is very slow. I want to assign this new attribute("my_attribute") to all attribute sets programmatically.
Can anyone can help me?
Thanks in advance.
Upvotes: 1
Views: 7045
Reputation: 4080
Additionally, Mage_Eav_Model_Entity_Setup
and Mage_Catalog_Model_Resource_Setup
, which extends from the aforementioned class, have all of the methods that you need to create attributes, sets, and groups. They are fairly straightforward, and they will help you understand how you're supposed to do it properly and prevent you from from writing bad or redundant code. I find most of the articles floating around the Internet and even Magento's own wiki entries feature poorly written code.
Upvotes: 0
Reputation: 531
$installer = Mage::getResourceModel('catalog/setup','default_setup');
$installer->startSetup();
$attributeCode = 'my_attribute';
$entity = Mage_Catalog_Model_Product::ENTITY;
// create a new attribute if it doesn't exist
$existingAttribute = $installer->getAttribute($entity, $attributeCode);
if (empty($existingAttribute)) {
$installer->addAttribute($entity, $attributeCode, array(<configure your attribute here>));
}
$attributeId = $installer->getAttributeId($entity, $attributeCode);
// add it to all attribute sets' default group
foreach ($installer->getAllAttributeSetIds($entity) as $setId) {
$installer->addAttributeToSet(
$entity,
$setId,
$installer->getDefaultAttributeGroupId($entity, $setId),
$attributeId
);
}
$installer->endSetup();
Upvotes: 3
Reputation: 3836
As Attributes are not assigned with any attribute set then, you can delete that attribute you created and then create required attribute programmatically.
In the Magento wiki's Programmatically Adding Attributes and Attribute Sets Section, It describes createAttribute
as the function that will solve your problem because it will create attributes and also assign them to attribute sets.
Hope This Helps!!
Upvotes: 1
Reputation: 23205
It's quite simple. In a custom module setup script:
$installer = Mage::getResourceModel('catalog/setup','default_setup');
$installer->startSetup();
$installer->addAttribute(
'catalog_product',
'your_attribute_code',
array(
'label' => 'Attribute Label',
'group' => 'General', // this will add to all attribute sets in the General group
// ...
)
)
$installer->endSetup();
For other utilities, see Mage_Eav_Model_Entity_Setup
.
Upvotes: 10
Reputation: 12809
Here's something quick, dirty, and untested :)
<?php
$attributeId = ID_HERE;
$installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter($entityType->getId());
foreach ($collection as $attributeSet) {
$attributeGroupId = $installer->getDefaultAttributeGroupId('catalog_product', $attributeSet->getId());
$installer->addAttributeToSet('catalog_product', $attributeSet->getId(), $attributeGroupId, $attributeId);
}
Upvotes: 4