Magento list of catalog product attributes and attributeset.
Magento list of catalog product attributes and attributeset.

Sometimes if you want some specific attribute and do some code based on that attribute then below code will give the exact solution for catalog product.So here I am going to show you How we can fetch all Magento list of catalog product attributes and attributeset .I mean I am going to show you how to fetch all attribute codes and name and with all attributes sets having name and Id.Generally this situations happens when we will try to add an attributes in catalog product section and that is not showing in our attribute section then we want to check th database section.But I you will be getting all by code then no need to go data base section

Also If you would like to check all attribute sets for a particular product then its better to check below code.And for getting all attribute sets store in entity attribute table then we can fetch by a simple way.All coding I have just done through custom script by means you can place your custom script inside root directory of your magento installation folder then run with magento base URL. So.So then after you will betting your all requirements.

custom script:

setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    try{
        $entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
        $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
        
        $allSet = array();
        foreach($collection as $coll){
           $attributeSet['name'] = $coll->getAttributeSetName();
           $attributeSet['id'] = $coll->getAttributeSetId();
           $allSet[] = $attributeSet;
        }
        
        Mage::log($allSet,1,'allSet.log');//make sure you have enabled log fron backend
        print_r($allSet);
        
        $attributeType = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY);
        $attributeCollection = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($attributeType);
        
        $allAttributes = array();
        foreach($attributeCollection as $coll){
            Mage::log($coll,1,'coll.log');
            $attribute['name'] = $coll->getAttributeCode();
            $attribute['id'] = $coll->getAttributeId();
            $allAttributes[] = $attribute;
        }
        
        Mage::log($allAttributes,1,'allAttributes.log');//make sure you have enabled log fron backend
        print_r($allAttributes);
        
    }catch(Exception $e){
        echo $e;
    }
?>

In the above example I have set the code to find out all attributes and attribute set details.So if we categories them individually then I can guess you will be understood better.

So below details are individual parts:

Attribute set:

getResource()->getTypeId();
    $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
    
    $allSet = array();
    foreach($collection as $coll){
        $attributeSet['name'] = $coll->getAttributeSetName();
        $attributeSet['id'] = $coll->getAttributeSetId();
        $allSet[] = $attributeSet;
    }
    
    Mage::log($allSet,1,'allSet.log');//make sure you have enabled log fron backend
    print_r($allSet);

?>
SELECT `main_table`.* FROM `eav_attribute_set` AS `main_table` WHERE (entity_type_id = '10');
    
            +------------------+----------------+--------------------+------------+
            | attribute_set_id | entity_type_id | attribute_set_name | sort_order |
            +------------------+----------------+--------------------+------------+
            |               44 |             10 | Cameras            |          0 |
            |               38 |             10 | Cell Phones        |          0 |
            |               39 |             10 | Computer           |          0 |
            |               60 |             10 | CPU                |          0 |
            |                9 |             10 | Default            |         16 |
            |               42 |             10 | Furniture          |          0 |
            |               62 |             10 | Hard Drive         |          0 |
            |               61 |             10 | Monitors           |          0 |
            |               58 |             10 | RAM                |          0 |
            |               46 |             10 | Shirts (General)   |          0 |
            |               45 |             10 | Shirts Other       |          0 |
            |               41 |             10 | Shirts T           |          0 |
            |               40 |             10 | Shoes              |          0 |
            |               59 |             10 | Warranties         |          0 |
            +------------------+----------------+--------------------+------------+
    
    14 rows in set (0.00 sec)

All attributes:

loadByCode(Mage_Catalog_Model_Product::ENTITY);
    $attributeCollection = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($attributeType);
    
    $allAttributes = array();
    foreach($attributeCollection as $coll){
        Mage::log($coll,1,'coll.log');
        $attribute['name'] = $coll->getAttributeCode();
        $attribute['id'] = $coll->getAttributeId();
        $allAttributes[] = $attribute;
    }
    
    Mage::log($allAttributes,1,'allAttributes.log');//make sure you have enabled log fron backend
    print_r($allAttributes);
?>

In all above parts I have set entityType to filter from database.like to fetch data details from table eav_entity_attribute table with filterable data for which entity type we want get data.

Magento list of catalog product attributes and attributeset
Tagged on:                     

Leave a Reply

Your email address will not be published. Required fields are marked *