Get all products by attribute set name or id in Magento

By | April 17, 2014

Here going to write a code snippet example, how you can fetch all products by it’s attribute set name or id :-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php 

//Fetch attribute set id by attribute set name
$attrSetName = 'your_attribute_set_name'; // your attribute name ex: diamond, watch etc
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
    ->load($attrSetName, 'attribute_set_name')
    ->getAttributeSetId();
 
//Load all products filtered by attribute set id 
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addFieldToFilter('attribute_set_id', $attributeSetId);
                OR
//Load only enable product filtered by attribute set id 
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addFieldToFilter('status', 1)
    ->addFieldToFilter('attribute_set_id', $attributeSetId);
    
 
//process your product collection
$productsName = array();
foreach($products as $_product){
    $productsName[] = $_product->getData('name');
}

//return all products name with attribute set 'your_attribute_set_name'
print_r($productsName); 

?>

Hope! you will enjoy :-) !!!

Leave a Reply