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

时间:2013-03-20 05:19:21

标签: magento

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

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

6 个答案:

答案 0 :(得分: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";
    }

答案 1 :(得分:2)

对于上述代码有问题的人,

like:在非对象上调用成员函数getModelInstance()

您需要将以下内容添加到文件顶部:

include 'app/Mage.php';
Mage::app();

编辑:

我正在使用magento 1.8.1.0并且代码仍无法正常工作

我必须将以下行添加到$ newItem,这样验证通过

    ->setAttributeCode($attCode)

答案 2 :(得分:2)

函数Mage_Catalog_Model_Resource_Setup::addAttribute()可用于更新和添加属性。我发现另一件有用的事情是,如果您使用此功能指定一个组,它将自动分配给所有集合。

$attributeCode = 'name'; // choose your attribute here
$setup = Mage::getResourceSingleton('catalog/setup');
$setup->addAttribute('catalog_product', $attributeCode, array(
    // no need to specify fields already used like 'label' or 'type'
    'group'      => 'General',
    'sort_order' => 10
));

答案 3 :(得分:1)

如果您使用的是外部脚本(不是magento设置脚本),那么这项工作将构成我

<?php
/// run in magento root
require_once 'app/Mage.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);
Mage::app();

$attributeCode = 'my_attr_code';
$group = 'MyGroup';
$sortOrder = 10;

//this way you config the setup connections
$setup = new Mage_Catalog_Model_Resource_Setup('catalog_setup');

$setup->startSetup();
$setup->addAttribute('catalog_product', $attributeCode, array(
    'group'      => $group,
    'sort_order' => $sortOrder
));

(作为对@Eric的回答)

答案 4 :(得分:0)

不要使用

Mage::getResourceSingleton('catalog/setup');

但请使用

Mage::getResourceModel('catalog/setup', 'catalog_setup');

答案 5 :(得分:0)

$attributeSets = Mage::getResourceModel('eav/entity_attribute_set_collection')
    ->setEntityTypeFilter('4'); // Catalog Product Entity Type ID
foreach ($attributeSets as $attributeSet) {
    $installer->addAttributeToSet(
        Mage_Catalog_Model_Product::ENTITY,   // Entity type
        $attributeSet->getAttributeSetName(), // Attribute set name
        'General',                            // Attribute set group name
        $yourAttributeCode,
        100                                   // Position on the attribute set group
    );
}