将现有属性添加到所有属性集 [英] Adding existing attribute to all attribute sets

查看:70
本文介绍了将现有属性添加到所有属性集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有嵌入代码的 existing 属性.我需要将此属性与120多个现有属性集关联.

I have an existing attribute for an embed code. I need to associate this attribute with 120+ existing attribute sets.

如果我知道属性集ID,该如何以编程方式将属性添加到所有属性集?

If I know the attribute set id, how can I go about adding the attribute to all attribute sets programmatically?

推荐答案

我发现为该问题编写代码很有趣,因此这是可行的解决方案:)

I have found it interesting to write code for this issue so here is the solution that works :)

在包括mage.php的php脚本中运行此代码,并让我知道它是否运行良好.

Run this code in php script including mage.php and let me know if it works well.

用要批量添加到所有属性集中的属性代码替换(名字)

replace ( firstname ) with the attribute code that you want to mass add to all attribute sets

    $attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity ( there is different entities in magento ex : catalog_category, order,invoice... etc ) 
    $attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity 
    $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
        ->setCodeFilter('firstname')
        ->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)->setOrder('attribute_group_id',"ASC")->getFirstItem();
        $groupId = $group->getId();
        $newItem = Mage::getModel('eav/entity_attribute');
        $newItem->setEntityTypeId($attSet->getId()) // catalog_product eav_entity_type id ( usually 10 )
                  ->setAttributeSetId($setId) // Attribute Set ID
                  ->setAttributeGroupId($groupId) // Attribute Group ID ( usually general or whatever based on the query i automate to get the first attribute group in each attribute set )
                  ->setAttributeId($attId) // Attribute ID that need to be added manually
                  ->setSortOrder(10) // Sort Order for the attribute in the tab form edit
                  ->save()
        ;
        echo "Attribute ".$attCode." Added to Attribute Set ".$set->getAttributeSetName()." in Attribute Group ".$group->getAttributeGroupName()."<br>\n";
    }

这篇关于将现有属性添加到所有属性集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆