1

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:

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

//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->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.

3

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();

$attribute_set_collection
->addFieldToFilter('entity_type_id',4)
->addFieldToFilter('attribute_set_name',$attrSetName);

$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();
$collection->addFieldToSelect("name");
$collection->addFieldToFilter('attribute_set_id',$attribute_set_id);

$productsName = array();
foreach ($collection as $item) {
    $productsName[] =  $item->getName();
}
print_r($productsName);
  • This worked like a charm, thank you soo much :) – Daniel Black Apr 27 '18 at 19:32
  • I am glad to hear this, you are welcome – WISAM HAKIM Apr 27 '18 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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