Magento – Create and add attribute group to all attribute sets

attributesce-1.6.2.0

I need to create and add an attribute group to all attributes sets in an existing install (CE 1.6.2).

I have found some scripts for creating an attribute group, but I can't seem to find anything on how to add that attribute group to all the existing sets.

Any help will be highly appreciated.

Best Answer

Create a new file on the same level as index.php. Let's call it groups.php with this content:

<?php
//enable errors to see if something is wrong
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
//include Mage.php
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
//tell magento that you are running on developer mode, for additional error messages (if any)
Mage::setIsDeveloperMode(true);
//instantiate the application
Mage::app();
//get the type ID of the product - you will need it later
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
//get all attribute sets
$sets = Mage::getModel('eav/entity_attribute_set')
    ->getResourceCollection()
    //filter only sets for products - that's why you needed the product type ID
    ->addFilter('entity_type_id', $entityTypeId);
//loop through all the sets
foreach ($sets as $set){
    //create an attribute group instance
    $modelGroup = Mage::getModel('eav/entity_attribute_group');
    //set the group name
    $modelGroup->setAttributeGroupName('Some group name here') //change group name
        //link to the current set
        ->setAttributeSetId($set->getId())
        //set the order in the set
        ->setSortOrder(100);
    //save the new group
    $modelGroup->save();
}

Save the file and access it in the browser: mysite.com/groups.php or run it in the command line:
> php /path/to/groups.php.
Now you should see the new group in all your attribute sets.
This can be done via an upgrade script, but it seams easier this way.