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>

Using the getUrl() Function to Display a Magento Base URL

Posted by Webrix | Posted in Magento | Posted on 08-02-2011

1

The quickest and easiest way to display your Magento Store Base URL anywhere on your store is to use the built in getURL() function.

<?php echo Mage::getUrl(); ?>

Result: http://www.domain.co.uk

Sometimes in Magento you may need to link to a category, product or even a CMS page. This can be achieved by doing the following.

<?php echo Mage::getUrl('custom-path.html'); ?>

Result: http://www.domain.co.uk/custom-path.html