Adding existing attribute to all attribute sets

前端 未结 6 1783
孤街浪徒
孤街浪徒 2021-02-06 05:53

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

If I know the attribute set id, ho

相关标签:
6条回答
  • 2021-02-06 06:42

    The function Mage_Catalog_Model_Resource_Setup::addAttribute() can be used to update as well as add attributes. Another thing I find useful is if you specify a group with this function it is automatically assigned to all sets.

    $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
    ));
    
    0 讨论(0)
  • 2021-02-06 06:44

    If you are using a external script (not the magento setups script) this work form me

    <?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
    ));
    

    (as answer to @Eric)

    0 讨论(0)
  • 2021-02-06 06:46

    Do not use

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

    But use

    Mage::getResourceModel('catalog/setup', 'catalog_setup');
    
    0 讨论(0)
  • 2021-02-06 06:50

    for people having problems with the code above,

    like: Call to a member function getModelInstance() on a non-object

    you need to add the following to to top of your file:

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

    edit:

    im using magento 1.8.1.0 and the code still didn't work

    i had to add the following line to $newItem, this way the validation passes

        ->setAttributeCode($attCode)
    
    0 讨论(0)
  • 2021-02-06 06:55

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

    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";
        }
    
    0 讨论(0)
  • 2021-02-06 06:55
    $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
        );
    }
    
    0 讨论(0)
提交回复
热议问题