Easy Batch Assign Custom Attributes Script

Submitted by metaharper - 4 years ago

Have you ever introduced a new custom attribute and find yourself having to apply it to numerous attributes sets one a time - dragging and dropping your way to an early grave? This easy script will allow you to mass assign unassigned attributes and save you countless hours. Use wisely and always make a database backup first!

// Step 1. Put this file in your Magento's web root
require_once 'app/Mage.php';
$attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem();
$attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); 
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')

// Step 2. Define your Attribute Code: e.g. belt_size, color, etc.
        ->setCodeFilter('ATTRIBUTE CODE')
$attCode = $attributeInfo->getAttributeCode();
$attId = $attributeInfo->getId();
foreach ($attSetCollection as $a) {
    $set = Mage::getModel('eav/entity_attribute_set')->load($a->getId());
        $setId = $set->getId();
// Step 3. Define your Attribute Group Name: e.g. General, Prices, Meta Information, etc.
        $group = Mage::getModel('eav/entity_attribute_group')->getCollection()->addFieldToFilter('attribute_set_id',$setId)->addFieldToFilter('attribute_group_name', 'ATTRIBUTE GROUP NAME')->setOrder('attribute_group_id',ASC)->getFirstItem();
        $groupId = $group->getId();
        $newItem = Mage::getModel('eav/entity_attribute');
// Step 4. Define your sort order (integer, no quotes necessary): i.e. Where your attribute field will appear in the product admin area
                  ->setSortOrder(SORT ORDER) 

// Step 5. Execute this script from the shell by running "php myscript.php" or just access it from a web browser if publicly accessible.
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean