8

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

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

4

6 回答 6

23

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

在包含 mage.php 的 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";
    }
于 2013-03-20T18:28:18.897 回答
2

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

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

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

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

编辑:

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

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

    ->setAttributeCode($attCode)
于 2014-01-31T12:40:32.420 回答
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
));
于 2014-08-29T17:40:23.687 回答
2

如果您使用的是外部脚本(而不是 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的回答)

于 2018-01-12T14:21:44.863 回答
0

不使用

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

但是使用

Mage::getResourceModel('catalog/setup', 'catalog_setup');
于 2018-01-05T13:39:26.817 回答
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
    );
}
于 2018-05-03T08:24:53.763 回答