将现有属性添加到所有属性集

Nic*_*ons 7 magento

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

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

Mea*_*bed 23

我发现为这个问题编写代码很有意思,所以这里的解决方案有效:)

在PHP脚本中运行此代码,包括mage.php,如果它运行良好,请告诉我.

将(firstname)替换为要批量添加到所有属性集的属性代码

    $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";
    }
Run Code Online (Sandbox Code Playgroud)

  • 简单的修复.调用`eav/entity_attribute_group`时,` - > addFieldToFilter('attribute_group_name','General')`. (3认同)
  • 更改选项卡中的位置 -&gt;setSortOrder(10) // 将此 10 更改为更高的数字! (2认同)
  • 谢谢!这在Magento EE 1.11.1.0上对我有用.我确实必须在引号中包含'ASC'参数,因为它触发了E_NOTICE. (2认同)
  • 对于需要这个的新手编码器,在代码`require_once('app/Mage.php')之前添加以下行; // Magento umask(0)的路径; 法师::应用程式();` (2认同)