I'm looking for a way to get all products that belong to a certain attribute set, say 'Gloves'. I have look around online to see if I could find a way but could only find the code below which I believe is for Magento 1 because I'm getting a 500 error code. Also it is a .phtml template where I'm trying to get this data.

This is the code I have tried:

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

//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
    ->addFieldToFilter('attribute_set_id', $attributeSetId);

//process your product collection as per your bussiness logic
$productsName = array();
foreach($products as $p){
    $productsName[] = $p->getData('name');

Any help or pointers would be greatly appreciated.

2 Answers 2


Please check this quick code, and try to optimize it as you wish,

$objectManager=   \Magento\Framework\App\ObjectManager::getInstance();

$attrSetName = 'Default';
$attribute_set_factoryCollection = $objectManager->get('\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory');

$attribute_set_collection = $attribute_set_factoryCollection->create();


$att_set = current($attribute_set_collection->getData());
$attribute_set_id = $att_set["attribute_set_id"];

$factoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $factoryCollection->create();

$productsName = array();
foreach ($collection as $item) {
    $productsName[] =  $item->getName();
  • This worked like a charm, thank you soo much :) Apr 27, 2018 at 19:32
  • I am glad to hear this, you are welcome Apr 27, 2018 at 20:35
  • its working perfectly Dec 18, 2019 at 6:13

The current accepted answer uses ObjectManager directly, which is discouraged. A newer approach to retrieving model instances uses repositories. Here is an example how you could make use of it for your scenario:

class GetProductNamesByAttributeSetName
    private $criteriaBuilder;
    private $attributeSets;
    private $products;

    public function __construct(
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Catalog\Api\AttributeSetRepositoryInterface $attributeSetRepository,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->criteriaBuilder = $searchCriteriaBuilder;
        $this->attributeSets = $attributeSetRepository;
        $this->products = $productRepository;

     * @param string $name
     * @return string[]
    public function execute($name)
        $setId = $this->getAttributeSetIdByName($name);
        if (empty($setId)) {
            return [];

        return array_map(
            fn ($product) => $product->getName(),

     * @param string $name
     * @return int|null
    private function getAttributeSetIdByName($name)
        $criteria = $this->criteriaBuilder->addFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ATTRIBUTE_SET_NAME, $name)->create();
        $sets = $this->attributeSets->getList($criteria)->getItems();
        return $sets? array_values($sets)[0]->getAttributeSetId() : null;

     * @param int $id
     * @return \Magento\Catalog\Api\Data\ProductInterface[]
    private function getProductsByAttributeSetId($id)
        $criteria = $this->criteriaBuilder->addFilter(\Magento\Catalog\Api\Data\ProductInterface::ATTRIBUTE_SET_ID, $id)->create();
        return $this->products->getList($criteria)->getItems();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.