1

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.

5 Answers 5

10

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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);
}

1 Comment

I just tested this and it worked however, make sure to require the app/Mage.php file.
3
$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();

Comments

1

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!!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.