Adding Attributes to Attribute Sets in bulk

Sometimes you need to make large changes after you are up and running. Luckily for us, if these are catalog changes, Magento has a nice bulk change tool to use. But when the change is adding a new attribute to your products attribute sets, there is no nice way to do that. At least not yet.

The other day, I had a client need to add a UPC attribute to all of her products. Her site uses the SKU value in a way that prevents us from just using that. So we needed to add a new attribute to all of the 300+ attribute sets. (If you have ever added an attribute to a set, you know that this is not hard, but will get tedious over a long set of items.)  So we needed a faster, and better way.

After a little bit of searching, I found this guy: Giles Bennet

He had the solution I needed. but I decided to make it a bit better.  First you will need to edit and drop a file into your Magento’s shell folder. This folder is right off of the Magento root, and has a couple of other command line executable php files.

I call mine “addAttribute.php” but it really doesn’t matter what you call it.

There are three variables at the top with comments. you have to edit the $attributeCode to tell the script what attribute you are adding.

After that, run it with “php addAttribute.php” if you used my file name, and wait for it to complete. Once it is done, you can go check out your new attributes.

Here is the code.

 

<?php
/*
* This script adds an attribute to all of the attributes sets
* Use with care
* This file should be located in the Magento-root/shell directory.
*/

//change the [attribute code] to be the code of the attribute you want to add
$attributeCode = ‘[Attribute Code]’;
//change this to be the group name in the attribute set where you want this attribute to go
$group = ‘General’;
//change this to the order location where you want this attribute to go
$sortOrder = ‘5’;

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($attributeCode)
->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’, $group)->setO
$groupId = $group->getId();
$newItem = Mage::getModel(‘eav/entity_attribute’);
$newItem->setEntityTypeId($attSet->getId())
->setAttributeSetId($setId)
->setAttributeGroupId($groupId)
->setAttributeId($attId)
->setSortOrder($sortOrder)
->save();
}
?>

 

Click to rate this post!
[Total: 0 Average: 0]
0

    Ravi

    May 3, 2014 - 12:50 am

    it shows an error

    Parse error: syntax error, unexpected ‘.’ in /home/shopingo/public_html/myshopee.in/add.php on line 15

    kindly help

    My Code :
    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($attributeCode)
    ->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’, $group)->setO
    $groupId = $group->getId();
    $newItem = Mage::getModel(‘eav/entity_attribute’);
    $newItem->setEntityTypeId($attSet->getId())
    ->setAttributeSetId($setId)
    ->setAttributeGroupId($groupId)
    ->setAttributeId($attId)
    ->setSortOrder($sortOrder)
    ->save();
    }
    ?>

    0
    Reply

      Roy

      May 16, 2014 - 7:39 am

      Ravi,

      Can you send me the add.php file to roy@theintegrationengineer.com?

      Also, let me know what version of Magento you are using.

      Thanks.

      I am betting there is a simple type-o ad a second set of eyes can find it.

      0
      Reply

    Matt

    July 17, 2014 - 5:08 am

    Your problem will be at:
    $group = Mage::getModel(‘eav/entity_attribute_group’)->getCollection()->addFieldToFilter(‘attribute_set_id’,$setId)->addFieldToFilter(‘attribute_group_name’, $group)->setO

    That line is incomplete both in the post and the reproduction.

    0
    Reply

Your email address will not be published. Required fields are marked *

*
*
*