Search By Attribute Block
Posted by Webrix | Posted in Magento | Posted on 09-02-2011
0
Have you ever wanted to use part of the the advanced search form to create a block on your home page so customers can search by product size, brand, colour etc? It is actually pretty easy to do as Magento does all the hard work for you.
In this example I will be explaining how to display a list of all product brands on the home page where customers can select one or more and be shown all products of the chosen brand(s).
Although Magento already has an attribute of manufacturer built in, we are going to create a new one called brand to show the process in full encase you need to use this for a custom attribute you are creating from scratch.
<?php
$attribute_name = "brand";
$select_size = "8";
?>
<form action="<?php echo Mage::getUrl('catalogsearch/advanced/result/'); ?>" method="get" id="form-validate" class="<?php echo $attribute_name; ?>-search">
<h2 class="legend">Search By Brand</h2>
<ul id="advanced-search-list">
<li>
<div class="input-box">
<?php
$attribute = Mage::getModel ( 'eav/config' )->getAttribute ( 'catalog_product', $attribute_name );
foreach ( $attribute->getSource ()->getAllOptions ( true, true ) as $option )
{
$attrubuteArray [$option ['value']] = $option ['label'];
}
?>
<select name="<?php echo $attribute_name; ?>[]" id="<?php echo $attribute_name; ?>" class="multiselect" title="<?php echo $attribute_name; ?>" multiple="multiple" size="<?php echo $select_size; ?>">
<?php
foreach($attrubuteArray as $key => $value)
{
if(!empty($key) && !empty($value)) echo "<option value=\"{$key}\">" . $value . "</option>";
}
?>
</select>
</div>
</li>
</ul>
<script type="text/javascript">decorateList('advanced-search-list')</script>
<button type="submit" title="Search" class="button" style="margin-bottom: 10px;"><span><span>Search</span></span></button>
</form>
