2

I'm trying to figure out how to identify stray attributes that do not belong to an attribute set.

We've got so many duplicate attributes, and we're having a hard time figuring out what is actually being used.

Also I heard that having lots of attributes will slow down the site. If we can delete the attributes we are not using, that would certainly help.

Please advise.

Regina Nickles
  • 301
  • 2
  • 8

1 Answers1

1

Please add bellow script on magento root. Eg file (xyz.php) and run this file in browser then results are attributes that do not belong to an attribute set.

<?php
require_once('app/Mage.php');
Mage::app('default');

class AR
{
    public function index()    
    {
        //$attributeSetIds = array(4);
        $attributeSetIds = Mage::getModel('catalog/product')->getDefaultAttributeSetId(); 
        $collection = Mage::getResourceModel('catalog/product_attribute_collection')
            ->setAttributeSetFilter($attributeSetIds)
            ->load();

        $attributesIds = array('0');
        /* @var $item Mage_Eav_Model_Entity_Attribute */
        foreach ($collection->getItems() as $item) {
            $attributesIds[] = $item->getAttributeId();
        }    

        $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
            ->setAttributesExcludeFilter($attributesIds)
            ->addVisibleFilter()
            ->load();

       $html .= '<h2>Product(s) Unassigned Attributes List</h2>';
       $html .= '<table style="border-style: solid solid solid; border-width: 1px 1px 1px; width: 100%;">
                <thead>
                    <tr>
                        <th>Attribute ID#</th>
                        <th>Attribute Label</th>
                        <th>Attribute Code</th>
                    </tr>
                </thead>
                <tbody>';
       if(count($attributes)) {
           foreach ($attributes as $attribute) {
               $html .= '<tr>';
               $html .= '<td align="center">'.$attribute->getAttributeId().'</td>';
               $html .= '<td align="center">'.$attribute->getFrontendLabel().'</td>';
               $html .= '<td align="center">'.$attribute->getAttributeCode().'</td>';
               $html .= '</tr>';
           }   
       }else {
          $html .= '<tr class="even"><td colspan="3" align="center">Attribute(s) not found.</td></tr>';
       }

       $html .= '</tbody></table>';
       echo $html;
       //$this->removeAttribute($code);
    }

    public function removeAttribute($code='')
    {
        $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
        $entityTypeId = (int) $installer->getEntityTypeId('catalog_product'); 
        $installer->startSetup();
        $installer->removeAttribute($entityTypeId ,$code);
        $installer->endSetup();
    }


}
$obj = new AR();
$obj->index();
?>

Also i have added remove attribute code in above script but right now it is commented

Note: Please always backup your database after use script.

Abdul
  • 9,701
  • 1
  • 20
  • 43