Magento : Display Products by Attribute set name

While writing the custom module I came across a situation where in there is need to display the custom products that is under specific attribute set, while searching on google I found lot many code snippets but following works for me

//Fetch attribute set id by attribute set name
$attrSetName = 'Diamonds';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
	->load($attrSetName, 'attribute_set_name')
	->getAttributeSetId();

//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
	->getCollection()
	->addAttributeToSelect(array('name')) // Here need to define the array of attributes to fetch, 
                                               //currently only name is fetched
	->addFieldToFilter('attribute_set_id', $attributeSetId);

//process your product collection as per your bussiness logic
$productsName = array();
foreach($products as $p){
	$productsName[] = $p->getData('name');
}
//return all products name with attribute set 'my_custom_attribute'

		Mage::register('data', $productsName);

To understand the above code you need the basic knowledge of magento framework for module development

Will write in detail about basics of module development in another series

Advertisements

What you think ? Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s