Introduction
An attribute set is a collection of attributes which can be created/edited from Catalog -> Manage Attribute Sets in backend.
Some useful snippets regarding Attribute Set.
1> Getting Attribute Set Details
1
2
3
4
|
$attributeSetModel = Mage::getModel( 'eav/entity_attribute_set' );
$attributeSetId = 9; // or $_product->getAttributeSetId(); if you are using product model
$attributeSetModel ->load( $attributeSetId );
print_r( $attributeSetModel ->getData());
|
2> Getting Attribute Set Id by Name
1
2
3
4
5
6
7
8
9
10
11
|
$entityTypeId = Mage::getModel( 'eav/entity' )
->setType( 'catalog_product' )
->getTypeId();
$attributeSetName = 'Default' ;
$attributeSetId = Mage::getModel( 'eav/entity_attribute_set' )
->getCollection()
->setEntityTypeFilter( $entityTypeId )
->addFieldToFilter( 'attribute_set_name' , $attributeSetName )
->getFirstItem()
->getAttributeSetId();
echo $attributeSetId ;
|
Note that the following code doesn’t work
1
2
3
|
$attributeSetId = Mage::getModel( 'eav/entity_attribute_set' )
->load( $attributeSetName , 'attribute_set_name' )
->getAttributeSetId();
|
Because if you refer to eav_attribute_set table then you can see there are lots of rows with same attribute set name(for example: Default)
3> Getting all Attribute Sets by Entity Type(catalog_product)
1
2
3
4
5
6
7
8
9
|
$entityTypeId = Mage::getModel( 'eav/entity' )
->setType( 'catalog_product' )
->getTypeId();
$attributeSetCollection = Mage::getModel( 'eav/entity_attribute_set' )
->getCollection()
->setEntityTypeFilter( $entityTypeId );
foreach ( $attributeSetCollection as $_attributeSet ){
print_r( $_attributeSet ->getData());
}
|
Alternatively, you can also use:
1
2
3
4
|
$attributeSets = Mage::getModel( 'catalog/product_attribute_set_api' )->items();
foreach ( $attributeSets as $_attributeSet ){
print_r( $_attributeSet );
}
|
4> Getting all attributes in an Attribute Set
1
2
3
4
|
$attributes = Mage::getModel( 'catalog/product_attribute_api' )->items( $attributeSetId );
foreach ( $attributes as $_attribute ){
print_r( $_attribute );
}
|
5> Filtering Products by Attribute Set
1
2
3
4
5
6
7
|
$productCollection = Mage::getModel( 'catalog/product' )
->getCollection()
->addAttributeToSelect( '*' )
->addFieldToFilter( 'attribute_set_id' , $attributeSetId );
foreach ( $productCollection as $_product ){
print_r( $_product ->getData());
}
|
Hope you will find these snippets useful/helpful.
Please do share if you have any useful code snippets regarding Attribute Set.
Thanks for reading!