5

I need to create and add an attribute group with 6 attributes to all attributes sets in an existing install.

I have found a script for creating an attribute group, this works great, but this can not add attributes to this new group.

Any help will be highly appreciated.

This script works great for adding an attribute group, but how can I add 6 attributes to a new group:

<?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();
}
11

It's a lot easier to do it via setup script. Here is an example:

// Add new Attribute group
$groupName = 'My Attr Group';
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$installer->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 100);
$attributeGroupId = $installer->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);

// Add existing attribute to group
$attributeId = $installer->getAttributeId($entityTypeId, ATTRIBUTE_CODE);
$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);

Note: $installer variable is instance of Mage_Eav_Model_Entity_Setup class. You can look there for further reference. You can also look in app/code/core/Mage/Catalog/sql/catalog_setup folder for some more examples.

  • I would be very grateful if you could share the full code, please!! – Artur Rain Aug 22 '18 at 10:19
  • addAttributeToGroup gets the groupId if the name of the group is provided, therefore you can use $groupName instead of $attributeGroupId in that function. It's just a precision, if install script does not work with this solution using name will not change anything. – Christophe Ferreboeuf Oct 3 '18 at 8:06
3

Using an installer script

Try

$filters = array(
    'my_attribute_id_1',
    'my_attribute_id_2'
     ..
);

$product_tab = 'General';

foreach($filters as $value){
    $eavModel    = Mage::getModel('eav/entity_setup','core_setup');
    $attributeId = $eavModel->getAttributeId('catalog_product', $value);

    foreach($eavModel->getAllAttributeSetIds('catalog_product') as $id) {
        $attributeGroupId = $eavModel->getAttributeGroupId('catalog_product', $id, $product_tab);
        $eavModel->addAttributeToSet('catalog_product', $id, $attributeGroupId, $attributeId);
    }
}
0

If you want to ...

  1. create new product attributes
  2. new attribute group for all sets
  3. asign attributes to related groups in all sets
  4. or modify existing attributes (group, position, ...)

... u can try this. I'd recommend to put it into a setup script for later reuse ...


/** @var Mage_Catalog_Model_Resource_Setup $setup */
$setup = Mage::getResourceModel('catalog/setup', 'default_setup');
$entity = Mage_Catalog_Model_Product::ENTITY;
$entityTypeId = $setup->getEntityTypeId($entity);

## config here:
$attributesDataMap = ... see below

foreach ($setup->getAllAttributeSetIds($entityTypeId) as $attributeSetId) {
    foreach ($attributesDataMap as $groupName => $groupData) {
        # add groups to all sets OR change position
        $setup->addAttributeGroup(
            $entityTypeId,
            $attributeSetId,
            $groupName,
            $groupData['sort_order']
        );

        if (count($groupData['attributes'])) {
            foreach ($groupData['attributes'] as $attributeCode => $attributeData) {
                $attributeId = $setup->getAttributeId($entityTypeId, $attributeCode);
                $attributeData = array_merge(
                    ['group' => $groupName],
                    $defaultData,
                    $groupData['default'],
                    $attributeData
                );
                # add new attribute
                if (!$attributeId) {
                    $setup->addAttribute(
                        $entityTypeId,
                        $attributeCode,
                        $attributeData
                    );
                # update attribute psotion and group
                } else {
                    $setup->updateAttribute(
                        $entityTypeId,
                        $attributeCode,
                        $attributeData
                    );
                    $setup->addAttributeToSet(
                        $entityTypeId,
                        $attributeSetId,
                        $groupName,
                        $attributeId,
                        $attributeData['sort_order']
                    );
                }
            }
        }
    }
}

Config:

Just a nested array in my case, that holds attribute group name, position and related attributes ...

$defaultData = [
    'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'backend'                       => '',
    'visible'                       => true,
    'searchable'                    => false,
    'filterable'                    => false,
    'comparable'                    => false,
    'is_configurable'               => false,
    'visible_on_front'              => false,
    'visible_in_advanced_search'    => false,
    'used_in_product_listing'       => false,
    'user_defined'                  => false,
    'required'                      => true,
];

$attributesDataMap = [
    'General' => [
        'sort_order' => null,
        'default' => $defaultData,
        'attributes' => [
            'ean' => [
                'label'         => 'EAN',
                'sort_order'    => '3',
            ],
            'mpn' => [
                'label'         => 'MPN',
                'sort_order'    => '4',
            ],
        ],
    ],
    'eBay' => [
        'sort_order' => 80,
        'default' => $defaultData,
        'attributes' => [
            'ebay_title' => [
                'label'             => 'eBay Titel',
                'sort_order'        => '10',
                'frontend_class'    => 'validate-length maximum-length-80',
                'note'              => 'Maximum 80 chars',
            ],
            ...
        ]
     ]
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.