简体   繁体   中英

Move a product attribute to a new group in magento admin

I created an attribute like so...

$installer = $this;
$installer->startSetup();

/* $installer Services_Issue_Model_Mysql4_Setup */
$installer->addAttribute('catalog_product', 'alice_id', array(
    'backend'                       => '',
    'frontend'                      => '',
    'type'                          => 'text',
    'visible'                       => true,
    'label'                         => 'Alice Id',
    'note'                          => 'Alice Id.',
    'input'                         => 'text',
    'unique'                        => true,
    'source'                        => '',
    'global'                        => true,
    'visible'                       => true,
    'required'                      => true,
    'user_defined'                  => true,
    'default'                       => '',
    'visible_on_front'              => true,
    'apply_to'                      => 'simple,configurable,default',
    'group'                         => 'Special Attributes',
    'used_in_product_listing'       => true,
    'frontend_class'                => '',
    'class'                         => '',
    'is_html_allowed_on_front'      => true,
    'searchable'                    => true
));

$installer->endSetup();

Now I need to move it to another group within the product information page. So I've tried this without any success.

$installer = $this;
$installer->startSetup();

/* $installer Services_Issue_Model_Mysql4_Setup */

$installer->updateAttribute('catalog_product', 'alice_id', 'note', 'Product SKU for Alice.com third-party cart & checkout.');

/* - move between groups not possible with updateAttribute - */

$installer->updateAttribute('catalog_product', 'alice_id', 'group', 'Additional Attributes');
$installer->endSetup();

Can anyone tell me how I can accomplish this?

You can use the addAttributeToGroup($entityType, $attributeSetId, $attributeGroupId, $attributeId, $sortOrder) method in Mage_Eav_Model_Entity_Setup to move an attribute to a different group. First, you'll need to get the set ID and group ID.

// ... start setup 

// get default set id
$setId = $installer->getDefaultAttributeSetId('catalog_product');

// get group id by name "Additional Attributes"
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
foreach ($attributeSetCollection->getData() as $attributeGroupIndex) {
    foreach ($attributeGroupIndex as $key => $value) {
        if ($key === "attribute_group_name" and $value === "Additional Attributes") {
            $groupId = $attributeGroupIndex["attribute_group_id"];
            break 2;
        }
    }
}

// move attribute 'alice_id' to group 'Additional Attributes'
if (isset($setId) and isset($groupId)) {
    $installer->addAttributeToGroup('catalog_product', $setId, $groupId, 'alice_id', 1000);
}

// ... end setup

If you know the name/code of your group, you can get it's group ID directly using getAttributeGroupID(). This example updates the group of attribute_code to be "Group Name" within the default attribute set.

// $installer->startSetup()
// ...

$eavSetup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$iDefaultAttrSetID = $eavSetup->getDefaultAttributeSetId('catalog_product');

$iAttributeID  = $eavSetup->getAttributeId('catalog_product', 'attribute_code');
$iGroupID = $eavSetup->getAttributeGroupId('catalog_product', $iDefaultAttrSetID, 'Group Name');
$eavSetup->addAttributeToGroup('catalog_product', $iDefaultAttrSetID, $iGroupID, $iAttributeID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM