2

Display all atrribute sets as a dropdown list in a static block.Then selecting one, display all the products from corresponding attribute set

2 Answers 2

5

You can get all attribute set list using below code.

$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection');
$attributeSetCollection->setEntityTypeFilter($entityType);
foreach ($attributeSetCollection as $attributeSet) {
    $name = $attributeSet->getAttributeSetName(); 
    $attributeSetId = $attributeSet->getId();
}

You can get Products from attribute set id as below

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('attribute_set_id', $attributeSetId);

foreach ($products as $child) {
    echo $child->getName();
}
2

One way to do this is to add search by attribute set to advanced search form.

in app/design/frontend/yourtheme/default/template/catalogsearch/advanced/form.phtml

You can get the dropdown using below code in the form

<li>
<?php
$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
?>

<label for="attribute_set_search_field">Search :</label>
<select name="attributeset" id="attribute_set_search_field">
  <option value="">-- Any --</option>
<?php foreach($collection as $coll): ?>
  <option value="<?php echo $coll->getAttributeSetId();?>"><?php echo $coll->getAttributeSetName();?></option>
<?php endforeach; ?>
</select>
</li>

Then in app/code/local/Mage/CatalogSearch/Model/Advanced.php

replace the getSearchCriterias() function with the code below

public function getSearchCriterias(){
    $search = $this->_searchCriterias;

    /* display attribute set filtering criteria */

    if(isset($_GET['attributeset']) && is_numeric($_GET['attributeset'])) {
        $entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
        $attributeSetId = $_GET['attributeset'];
        $myattriset = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter($entityType)
            ->addFieldToFilter('attribute_set_id', $attributeSetId)
            ->getFirstItem();
        $search[] = array('name'=>'Attribute set','value'=>$myattriset->getAttributeSetName());
    }

    return $search;
}

then replace the next function, getProductCollection(), with:

public function getProductCollection(){
    if (is_null($this->_productCollection)) {
        $collection = $this->_engine->getAdvancedResultCollection();
        $this->prepareProductCollection($collection);
        if (!$collection) {
            return $collection;
        }
        $this->_productCollection = $collection;

    /* include attribute set filtering */
        if(isset($_GET['attributeset']) && is_numeric($_GET['attributeset']))
            $this->_productCollection
                 ->addFieldToFilter('attribute_set_id',$_GET['attributeset']);

    }

    return $this->_productCollection;
}

Then in function addFilters($values) you can see following code

if ($allConditions) {
        $this->getProductCollection()->addFieldsToFilter($allConditions);
    } else if (!count($filteredAttributes)) {
        Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
    } 

Replace with

if (($allConditions) || (isset($values['attributeset']) && is_numeric($values['attributeset']))) {
        $this->getProductCollection()->addFieldsToFilter($allConditions);
    } else if (!count($filteredAttributes)) {
        Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
    }

Now you can search product by attribute set in your advanced search form

You can also use the code below in any template file

<form action="<?php echo $this->getUrl('catalogsearch/advanced/result') ?>" method="get" id="form-validate">

<?php
$entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
$collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
?>

<label for="attribute_set_search_field">Search :</label>
<select name="attributeset" id="attribute_set_search_field">
  <option value="">-- Any --</option>
<?php foreach($collection as $coll): ?>
  <option value="<?php echo $coll->getAttributeSetId();?>"><?php echo $coll->getAttributeSetName();?></option>
<?php endforeach; ?>
</select>

</form>

You can also duplicate your advanced search form to make changes there, following this tutorial http://inchoo.net/magento/advanced-search-in-magento-and-how-to-use-it-in-your-own-way/

Thanks to this tutorial that gives the same answer for adding search by category https://magento2.atlassian.net/wiki/display/m1wiki/How+to+Add+Search+By+Category+to+Advanced+Search

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.