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

标签 magento

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

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

最佳答案

我发现针对这个问题编写代码很有趣,所以这里是有效的解决方案:)

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

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";
    }

关于magento - 将现有属性添加到所有属性集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15515932/

相关文章:

php - Magento - 检查管理员和客户是否登录

php - Magento - 向 sales_flat_quote_item 和 sales_flat_order_item 添加新列

html - MySQL 更新查询以从所有 "description"字段中删除特定的 Html 标记

jquery - Magento #page-login

java - magento xmlrpc session 立即过期

php - 具有自定义属性的 Magento 产品网格过滤器

magento - 什么会导致 redis 保留旧 key ?

php - GA 不跟踪某些操作

android - magento 网站的 android 应用程序的支付网关集成

Magento 电子商务无法生成报告